Oj Strict Mode Performance

peter@ohler.com

Author: Peter Ohler Published: June 16, 2013

Optimized JSON (Oj) is a C extension to Ruby. The Oj parser in strict mode has been optimized to the point where, the non-Ruby C code now contributes only 10% (8% or less for large JSON and 12% for small JSON documents) to the overall time it takes to parse and generate the Ruby objects represented in a JSON document.

In addition to being the fastest Ruby JSON parser it also uses a constant stack size no matter how deeply nested the JSON document is. Oj does this by using a single pass, non-recursive callback parser. Minimizing stack use is important if Ruby fibers are used as the default stack size is small.

To benchmark the Oj strict parser a Ruby Object was selected that included all the various types that can be expected in a JSON document as well as some nested arrays and hashes to add some variety. The file used for generating the benchmark results is in GitHub. The JSON used for the tests is:

{"a":"Alpha","b":true,"c":12345,"d":[true,[false,[-123456789,null],3.9676,["Something else.",false],null]],"e":{"zero":null,"one":1,"two":2,"three":[3],"four":[0,1,2,3,4]},"f":null,"h":{"a":{"b":{"c":{"d":{"e":{"f":{"g":null}}}}}}},"i":[[[[[[[null]]]]]]]}

With that JSON as a building block, longer JSON document are created by repeating that structure in an array. That allows benchmarking larger JSON documents as well as the smaller one.

Benchmarks were done against the latest version of yajl-ruby 1.1.0 and json 1.8.0. Json_pure was not included as it is much slower than Json::Ext and does not support convertion from and to numbers as evident from the benchmark test.

Results

In the tables, a higher ratio is better. The ratio is a comparison to the slowest parser in the test. The times listed are in seconds and are the number of seconds it took to parse the number of iterations noted in the test title.

Small: 100,000 parses of a 255 byte JSON

Gem

Parse
Time

Rate

Ratio

Oj

1.31 seconds

76.6K parses/sec

1.70

JSON::Ext

2.08 seconds

48.0K parses/sec

1.10

Yajl

2.29 seconds

43.8K parses/sec

1.00

Both Yajl and the Json extension gems are fairly close in performance while Oj stand out by being 60 to 70% faster for the small JSON document tested.

Small Parse Results

Oj
Json 
Yajl 

Large: 100 parses of a 1000K byte JSON

Gem

Parse
Time

Rate

Ratio

Oj

7.14 seconds

14.0 parses/sec

1.51

Yajl

8.57 seconds

11.7 parses/sec

1.26

JSON::Ext

10.8 seconds

9.3 parses/sec

1.00

For larger files of 1M the results change considerably. Yajl is 26% faster than the Json gem while Oj remains the fastest at 51% faster than the Json gem and 20% faster than the Yajl gem.

Large Parse Results

Oj
Yajl 
Json