Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Timing JSON modules
#!/usr/bin/env perl
use strict;
use warnings;
use Try::Tiny;
use Benchmark qw(cmpthese);
use JSON::XS ();
use Cpanel::JSON::XS ();
use JSON::Tiny;
use Mojo::JSON;
use JSON::PP ();
use JSON::DWIW ();
my $data = {
a => 1,
b => 'foo',
c => [1, 'bar'],
d => { a => 1, b => 'foo' },
e => undef,
};
cmpthese -2, {
cpjxs => sub {
my $j = Cpanel::JSON::XS->new();
my $jdata = $j->encode($data);
$j->decode($jdata);
},
jxs => sub {
my $j = JSON::XS->new();
my $jdata = $j->encode($data);
$j->decode($jdata);
},
jt => sub {
my $j = JSON::Tiny->new();
my $jdata = $j->encode($data);
$j->decode($jdata);
},
mj => sub {
my $j = Mojo::JSON->new();
my $jdata = $j->encode($data);
$j->decode($jdata);
},
jpp => sub {
my $j = JSON::PP->new();
my $jdata = $j->encode($data);
$j->decode($jdata);
},
jdwiw => sub {
my $j = JSON::DWIW->new();
my $jdata = $j->to_json($data);
$j->from_json($jdata);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment