Skip to content

Instantly share code, notes, and snippets.

@kfly8
Created September 14, 2020 13:55
Show Gist options
  • Select an option

  • Save kfly8/57b02767ef93ac19d3f5231a8bc955d8 to your computer and use it in GitHub Desktop.

Select an option

Save kfly8/57b02767ef93ac19d3f5231a8bc955d8 to your computer and use it in GitHub Desktop.
JSON::Types vs Cpanel::JSON::XS::Type
use v5.32.0;
use warnings;
use Benchmark qw(cmpthese);
use JSON::XS ();
use Cpanel::JSON::XS ();
use Cpanel::JSON::XS::Type;
use JSON::Types;
use constant Item => {
id => JSON_TYPE_INT,
name => JSON_TYPE_STRING,
fg => JSON_TYPE_BOOL,
};
use constant Items => {
items => json_type_arrayof(Item),
count => JSON_TYPE_INT,
};
say "perl version - $]";
say "JSON::XS - ${JSON::XS::VERSION}";
say "Cpanel::JSON::XS - ${Cpanel::JSON::XS::VERSION}";
say "JSON::Types - ${JSON::Types::VERSION}";
say 'Encode small object';
cmpthese -1, {
json_xs_with_json_types => sub {
my $obj = {
count => number 123,
items => [
{
id => number 1,
name => string 'foo',
fg => bool 1,
},
{
id => number 2,
name => string 'bar',
fg => bool 0,
},
{
id => number 3,
name => string 'baz',
fg => bool 1,
},
{
id => number 4,
name => string 'baz',
fg => bool 0,
},
{
id => number 5,
name => string 'boo',
fg => bool 1,
}
],
};
JSON::XS::encode_json($obj);
},
json_xs_with_json_types_lazy => sub {
my $obj = {
count => number 123,
items => [
{
id => number 1,
name => 'foo',
fg => bool 1,
},
{
id => number 2,
name => 'bar',
fg => bool 0,
},
{
id => number 3,
name => 'baz',
fg => bool 1,
},
{
id => number 4,
name => 'baz',
fg => bool 0,
},
{
id => number 5,
name => 'boo',
fg => bool 1,
}
],
};
JSON::XS::encode_json($obj);
},
cpanel_json_with_types => sub {
my $obj = {
count => 123,
items => [
{
id => 1,
name => 'foo',
fg => 1,
},
{
id => 2,
name => 'bar',
fg => 0,
},
{
id => 3,
name => 'baz',
fg => 1,
},
{
id => 4,
name => 'baz',
fg => 0,
},
{
id => 5,
name => 'boo',
fg => 1,
}
],
};
Cpanel::JSON::XS::encode_json($obj, Items);
},
cpanel_json_but_wrong => sub {
my $obj = {
count => 123,
items => [
{
id => 1,
name => 'foo',
fg => 1,
},
{
id => 2,
name => 'bar',
fg => 0,
},
{
id => 3,
name => 'baz',
fg => 1,
},
{
id => 4,
name => 'baz',
fg => 0,
},
{
id => 5,
name => 'boo',
fg => 1,
}
],
};
Cpanel::JSON::XS::encode_json($obj);
},
json_xs_but_wrong => sub {
my $obj = {
count => 123,
items => [
{
id => 1,
name => 'foo',
fg => 1,
},
{
id => 2,
name => 'bar',
fg => 0,
},
{
id => 3,
name => 'baz',
fg => 1,
},
{
id => 4,
name => 'baz',
fg => 0,
},
{
id => 5,
name => 'boo',
fg => 1,
}
],
};
JSON::XS::encode_json($obj);
}
}
@kfly8
Copy link
Author

kfly8 commented Sep 14, 2020

perl version - 5.032000
JSON::XS - 4.02
Cpanel::JSON::XS - 4.21
JSON::Types - 0.05

Encode small object

# 1st.
                                 Rate json_xs_with_json_types json_xs_with_json_types_lazy cpanel_json_with_types cpanel_json_but_wrong json_xs_but_wrong
json_xs_with_json_types      130327/s                      --                         -15%                   -18%                  -27%              -36%
json_xs_with_json_types_lazy 153121/s                     17%                           --                    -4%                  -14%              -25%
cpanel_json_with_types       159288/s                     22%                           4%                     --                  -10%              -21%
cpanel_json_but_wrong        177535/s                     36%                          16%                    11%                    --              -12%
json_xs_but_wrong            202867/s                     56%                          32%                    27%                   14%                --


# 2nd.
                                 Rate json_xs_with_json_types cpanel_json_with_types json_xs_with_json_types_lazy cpanel_json_but_wrong json_xs_but_wrong
json_xs_with_json_types      122529/s                      --                   -11%                         -18%                  -21%              -34%
cpanel_json_with_types       137186/s                     12%                     --                          -8%                  -11%              -26%
json_xs_with_json_types_lazy 149592/s                     22%                     9%                           --                   -3%              -19%
cpanel_json_but_wrong        154566/s                     26%                    13%                           3%                    --              -17%
json_xs_but_wrong            185579/s                     51%                    35%                          24%                   20%                --

# 3rd.
                                 Rate json_xs_with_json_types json_xs_with_json_types_lazy cpanel_json_with_types cpanel_json_but_wrong json_xs_but_wrong
json_xs_with_json_types      127242/s                      --                         -18%                   -19%                  -25%              -34%
json_xs_with_json_types_lazy 154983/s                     22%                           --                    -1%                   -8%              -20%
cpanel_json_with_types       156392/s                     23%                           1%                     --                   -8%              -19%
cpanel_json_but_wrong        169239/s                     33%                           9%                     8%                    --              -12%
json_xs_but_wrong            192752/s                     51%                          24%                    23%                   14%                --

# 4th.
                                 Rate json_xs_with_json_types json_xs_with_json_types_lazy cpanel_json_with_types cpanel_json_but_wrong json_xs_but_wrong
json_xs_with_json_types      130326/s                      --                         -17%                   -18%                  -24%              -33%
json_xs_with_json_types_lazy 156392/s                     20%                           --                    -2%                   -8%              -19%
cpanel_json_with_types       159288/s                     22%                           2%                     --                   -7%              -18%
cpanel_json_but_wrong        170666/s                     31%                           9%                     7%                    --              -12%
json_xs_but_wrong            193729/s                     49%                          24%                    22%                   14%                --

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment