Oj Compat Mode Performance |
|
Author: Published: |
Optimized JSON (Oj) is a C extension to Ruby. The Oj parser in compat 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 compat 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,"g":{"json_class":"One::Two::Three::Empty"},"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 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. Yajl was not included as it does not support the feature of encoding Object that respond to the to_json() or to_hash() method and does not support the create_id feature. 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 299 byte JSON
|
Small Parse Results
|
||||||||||||||||||
Large: 100 parses of a 1000K byte JSON
For larger files of 1M the difference between Json and Oj is not as great as for small json document but Oj is still over twice as fast. |
Large Parse Results
|