Archive for May, 2014

Perl and JSON

Saturday, May 31st, 2014

I like JSON a lot when it comes to seralizing data and REST web services. Perl has very good support for JSON and several libraries to pick from. While selecting the best components for my projects, I have been testing and timing a few Perl modules that produce JSON.┬áThese are JSON::PP, which has been in the Perl core since 5.13.9, Mojo::JSON, part of the excellent Mojolicious web framework, JSON::Tiny, a stand-alone adaptation of the latter, JSON::XS, written in C for blazing speed and Cpanel::JSON::XS, a fork of the latter. I also tested JSON:DWIW, an older module also written in C. Let’s see how they perform.

Rate jpp jt mj jdwiw jxs cpjxs
jpp 4437/s -6% -11% -82% -97% -97%
jt 4718/s 6% -5% -81% -97% -97%
mj 4977/s 12% 5% -80% -97% -97%
jdwiw 24917/s 462% 428% 401% -83% -85%
jxs 145479/s 3179% 2983% 2823% 484% -10%
cpjxs 161266/s 3534% 3318% 3140% 547% 11%

If you don’t want to bother, JSON::MaybeXS is a wrapper module that automatically selects the best out of three of them and can also operate like the core JSON, so you only have to change your use statement and leave the rest of your code untouched. JSON::PP,┬áJSON:DWIW and the XS modules are packed with features, while Mojo::JSON and JSON::Tiny are minimalistic libraries and only produce inline compacted JSON suitable for machine consumption.