use v5.20; use warnings; use Benchmark qw( :hireswallclock cmpthese ); use JSON::PP (); use JSON::XS (); use Cpanel::JSON::XS (); my $json_pp= JSON::PP->new; my $json_xs= JSON::XS->new; my $json_cxs= Cpanel::JSON::XS->new; my $small_json_string= q|{"test":1}|; sub random_data { return { map { rand() => $_[0]? random_data($_[0]-1) : rand() } 1..40 } } my $long_json_string= $json_xs->encode(random_data(2)); say "long_json: ".length $long_json_string; cmpthese(1000000, { pp_small_eval => sub { eval { !$json_pp->decode($small_json_string) } }, xs_small_eval => sub { eval { !$json_xs->decode($small_json_string) } }, cxs_small_eval => sub { eval { !$json_cxs->decode($small_json_string) } }, pp_small => sub { !$json_pp->decode($small_json_string) }, xs_small => sub { !$json_xs->decode($small_json_string) }, cxs_small => sub { !$json_cxs->decode($small_json_string) }, }); cmpthese(40, { pp_long_eval => sub { eval { !$json_pp->decode($long_json_string) } }, xs_long_eval => sub { eval { !$json_xs->decode($long_json_string) } }, cxs_long_eval => sub { eval { !$json_cxs->decode($long_json_string) } }, pp_long => sub { !$json_pp->decode($long_json_string) }, xs_long => sub { !$json_xs->decode($long_json_string) }, cxs_long => sub { !$json_cxs->decode($long_json_string) }, });