Ruby json benchmark
Ruby Shell
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
gemfiles Oj 3.0.6 May 7, 2017
lib update test results May 8, 2017
.gitignore New Oj Apr 28, 2017
.travis.yml Oj 3.0.6 May 7, 2017
Gemfile New Oj Apr 28, 2017
Gemfile.lock New Oj Apr 28, 2017
README.md Updated benchmark May 4, 2017
benchmark.rb update test results May 8, 2017
compatibility_test.rb Updated benchmark May 4, 2017
rails_benchmark.rb Oj 3.0.6 May 7, 2017
test_report.txt update test results May 8, 2017
update_bundle.sh Oj 3.0.6 May 7, 2017

README.md

Benchmark JSON implementations in Ruby

Why

to_json in Rails project is not the same as JSON.generate or to_json in non-Rails project. See benchmarks Rails to_json + Oj.mimic_JSON vs No Rails to_json + Oj.mimic_JSON (below).

Compatibility test

Comparing Rails to_json with other JSON implementations:

bundle exec ruby compatibility_test.rb
Comparing Rails to_json with other JSON implementations
+---------------------------------+--------------+---------+------------------+------------+-----------------+
| class                           | JSON to_json | Oj.dump | Oj::Rails.encode | Oj to_json | msgpack "rails" |
+---------------------------------+--------------+---------+------------------+------------+-----------------+
| Regexp                          | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| FalseClass                      | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| NilClass                        | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Object                          | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| TrueClass                       | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| String                          | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| StringChinese                   | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| StringSpecial                   | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| StringSpecial2                  | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| StringSpecial3                  | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Numeric                         | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Symbol                          | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Time                            | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Array                           | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Hash                            | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| HashNotEmpty                    | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Date                            | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| DateTime                        | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Enumerable                      | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| BigDecimal                      | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| BigDecimalInfinity              | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Struct                          | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Float                           | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| FloatInfinity                   | πŸ’€           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | ❌              |
| Range                           | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Complex                         | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Exception                       | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| OpenStruct                      | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Rational                        | πŸ‘Œ           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| Process::Status                 | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| ActiveSupport::TimeWithZone     | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| ActiveModel::Errors             | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| ActiveSupport::Duration         | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| ActiveSupport::Multibyte::Chars | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| ActiveRecord::Relation          | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
| ActiveRecord                    | ❌           | πŸ‘Œ      | πŸ‘Œ               | πŸ‘Œ         | πŸ‘Œ              |
+---------------------------------+--------------+---------+------------------+------------+-----------------+

Note: msgpack is not JSON, so comparing results of serialization + deserialization e.g.

ActiveSupport::JSON.decode(obj.to_json) vs MessagePack.unpack(obj.to_msgpack)

See comparison across Ruby/Rails version in test_report.txt. Report was generated with command: wwtd &> test_report.txt.

Tests based on as_json implementations.

See also:

Benchmark Rails to_json vs Oj.dump

Calculating -------------------------------------
            to_json:   340.131M memsize (   168.000  retained)
                         6.660M objects (     2.000  retained)
                        50.000  strings (     0.000  retained)
           Oj.dump o    55.880M memsize (     0.000  retained)
                       990.000k objects (     0.000  retained)
                        38.000  strings (     0.000  retained)
           Oj.dump c    55.880M memsize (     0.000  retained)
                       990.000k objects (     0.000  retained)
                        38.000  strings (     0.000  retained)
       Oj.dump c, aj    55.880M memsize (     0.000  retained)
                       990.000k objects (     0.000  retained)
                        38.000  strings (     0.000  retained)

Comparison:
       Oj.dump c, aj:   55880000 allocated
           Oj.dump o:   55880000 allocated - same
           Oj.dump c:   55880000 allocated - same
            to_json::  340130720 allocated - 6.09x more
---------------------------------------------

                     user     system      total        real
to_json:         2.810000   0.170000   2.980000 (  3.048407)
Oj.dump o        0.630000   0.020000   0.650000 (  0.666360)
Oj.dump c        0.440000   0.010000   0.450000 (  0.459752)
Oj.dump c, aj    0.530000   0.020000   0.550000 (  0.554771)

Rails to_json + Oj.mimic_JSON

bundle exec ruby benchmark1.rb
Calculating -------------------------------------
            to_json:    18.730M memsize (     0.000  retained)
                       270.000k objects (     0.000  retained)
                         3.000  strings (     0.000  retained)
               JSON:     2.970M memsize (     0.000  retained)
                        20.000k objects (     0.000  retained)
                         1.000  strings (     0.000  retained)
                 Oj:     2.970M memsize (     0.000  retained)
                        20.000k objects (     0.000  retained)
                         1.000  strings (     0.000  retained)

Comparison:
                 Oj::    2970000 allocated
               JSON::    2970000 allocated - same
            to_json::   18730000 allocated - 6.31x more <---- PAY ATTENTION

---------------------------------------------

                     user     system      total        real
to_json:         0.080000   0.000000   0.080000 (  0.079702)
JSON:            0.010000   0.000000   0.010000 (  0.012702)
Oj:              0.020000   0.000000   0.020000 (  0.015271)

No Rails to_json + Oj.mimic_JSON

bundle exec ruby benchmark1.rb
Calculating -------------------------------------
            to_json:     2.970M memsize (     0.000  retained)
                        20.000k objects (     0.000  retained)
                         1.000  strings (     0.000  retained)
               JSON:     2.970M memsize (     0.000  retained)
                        20.000k objects (     0.000  retained)
                         1.000  strings (     0.000  retained)
                 Oj:     2.970M memsize (     0.000  retained)
                        20.000k objects (     0.000  retained)
                         1.000  strings (     0.000  retained)

Comparison:
            to_json::    2970000 allocated
               JSON::    2970000 allocated - same
                 Oj::    2970000 allocated - same

---------------------------------------------

                     user     system      total        real
to_json:         0.020000   0.000000   0.020000 (  0.014124)
JSON:            0.020000   0.010000   0.030000 (  0.024667)
Oj:              0.020000   0.000000   0.020000 (  0.028081)