Skip to content

Instantly share code, notes, and snippets.

@NullVoxPopuli
Last active October 9, 2019 20:30
Show Gist options
  • Save NullVoxPopuli/748e89ddc1732b42fdf42435d773734a to your computer and use it in GitHub Desktop.
Save NullVoxPopuli/748e89ddc1732b42fdf42435d773734a to your computer and use it in GitHub Desktop.
ActiveModelSerializers vs jsonapi-rb Benchmarks

Deserialization

deserialization_benchmark.rb

Each of these scenarios just changes the payload to be parsed, and does not change any deserialization options.

No Relationships

Calculating -------------------------------------
          ams            13.415k (± 0.8%) i/s -    135.160k in  10.095866s
          jsonapi-rb     18.495k (± 1.7%) i/s -    182.413k in  10.100698s
          custom        221.752k (± 1.3%) i/s -      2.175M in   9.981512s
                   with 95.0% confidence

Comparison:
          custom    :   221752.5 i/s
          jsonapi-rb:    18494.6 i/s - 11.99x  (± 0.27) slower
          ams       :    13415.1 i/s - 16.53x  (± 0.26) slower
                   with 95.0% confidence

Calculating -------------------------------------
          ams           10.296k memsize (     0.000  retained)
                        74.000  objects (     0.000  retained)
                         4.000  strings (     0.000  retained)
          jsonapi-rb    10.744k memsize (     0.000  retained)
                        91.000  objects (     0.000  retained)
                        31.000  strings (     0.000  retained)
          custom       640.000  memsize (     0.000  retained)
                         5.000  objects (     0.000  retained)
                         3.000  strings (     0.000  retained)

Comparison:
          custom    :        640 allocated
          ams       :      10296 allocated - 16.09x more
          jsonapi-rb:      10744 allocated - 16.79x more

One belongs_to

Calculating -------------------------------------
          ams             7.702k (± 0.8%) i/s -     77.147k in  10.045611s
          jsonapi-rb      9.791k (± 1.2%) i/s -     97.350k in  10.069045s
          custom        147.919k (± 1.6%) i/s -      1.461M in  10.042105s
                   with 95.0% confidence

Comparison:
          custom    :   147918.8 i/s
          jsonapi-rb:     9791.5 i/s - 15.11x  (± 0.31) slower
          ams       :     7701.6 i/s - 19.21x  (± 0.35) slower
                   with 95.0% confidence

Calculating -------------------------------------
          ams           11.968k memsize (    40.000  retained)
                        93.000  objects (     1.000  retained)
                         6.000  strings (     1.000  retained)
          jsonapi-rb    12.822k memsize (    80.000  retained)
                       122.000  objects (     2.000  retained)
                        39.000  strings (     2.000  retained)
          custom       840.000  memsize (     0.000  retained)
                        10.000  objects (     0.000  retained)
                         7.000  strings (     0.000  retained)

Comparison:
          custom    :        840 allocated
          ams       :      11968 allocated - 14.25x more
          jsonapi-rb:      12822 allocated - 15.26x more

2 belongs_to

Calculating -------------------------------------
          ams             5.675k (± 0.7%) i/s -     56.784k in  10.029038s
          jsonapi-rb      6.814k (± 0.9%) i/s -     68.234k in  10.079003s
          custom        111.095k (± 1.5%) i/s -      1.101M in  10.019325s
                   with 95.0% confidence

Comparison:
          custom    :   111095.3 i/s
          jsonapi-rb:     6813.6 i/s - 16.30x  (± 0.30) slower
          ams       :     5674.9 i/s - 19.58x  (± 0.33) slower
                   with 95.0% confidence

Calculating -------------------------------------
          ams           13.520k memsize (    40.000  retained)
                       109.000  objects (     1.000  retained)
                         8.000  strings (     1.000  retained)
          jsonapi-rb    14.776k memsize (   120.000  retained)
                       150.000  objects (     3.000  retained)
                        43.000  strings (     3.000  retained)
          custom         1.040k memsize (     0.000  retained)
                        15.000  objects (     0.000  retained)
                         9.000  strings (     0.000  retained)

Comparison:
          custom    :       1040 allocated
          ams       :      13520 allocated - 13.00x more
          jsonapi-rb:      14776 allocated - 14.21x more

has_many, no belongs_to

I don't think AMS deserializes has_many relationships (at least by default)

Calculating -------------------------------------
          ams             6.618k (± 0.6%) i/s -     66.465k in  10.052403s
          jsonapi-rb      4.591k (± 1.2%) i/s -     45.430k in  10.083374s
          custom          4.726k (± 0.9%) i/s -     47.066k in  10.015956s
                   with 95.0% confidence

Comparison:
          ams       :     6617.9 i/s
          custom    :     4726.5 i/s - 1.40x  (± 0.02) slower
          jsonapi-rb:     4590.8 i/s - 1.44x  (± 0.02) slower
                   with 95.0% confidence

Calculating -------------------------------------
          ams           13.256k memsize (    40.000  retained)
                       104.000  objects (     1.000  retained)
                         7.000  strings (     1.000  retained)
          jsonapi-rb    15.106k memsize (    80.000  retained)
                       149.000  objects (     2.000  retained)
                        39.000  strings (     2.000  retained)
          custom         3.936k memsize (     0.000  retained)
                        40.000  objects (     0.000  retained)
                         9.000  strings (     0.000  retained)

Comparison:
          custom    :       3936 allocated
          ams       :      13256 allocated - 3.37x more
          jsonapi-rb:      15106 allocated - 3.84x more

Serialization of ActiveRecord objects

serialization_benchmark.rb

Large Data Set

User

  • 20 posts
    • 20 comments per post

This also tests eager_loading, because AMS disregards eager_loaded data...

Calculating -------------------------------------
ams                        
                          2.873  (± 2.5%) i/s -     29.000  in  10.165212s
jsonapi-rb                 
                         14.706  (± 0.7%) i/s -    147.000  in  10.021414s
ams        eager           
                          2.944  (± 2.1%) i/s -     30.000  in  10.247200s
jsonapi-rb eager           
                         15.561  (± 1.0%) i/s -    155.000  in  10.023231s
                   with 95.0% confidence

Comparison:
jsonapi-rb eager           :       15.6 i/s
jsonapi-rb                 :       14.7 i/s - 1.06x  (± 0.01) slower
ams        eager           :        2.9 i/s - 5.28x  (± 0.12) slower
ams                        :        2.9 i/s - 5.41x  (± 0.15) slower
                   with 95.0% confidence

Calculating -------------------------------------
ams                        
                        11.385M memsize (   841.082k retained)
                       126.244k objects (    10.153k retained)
                        50.000  strings (    50.000  retained)
jsonapi-rb                 
                         4.249M memsize (     0.000  retained)
                        41.965k objects (     0.000  retained)
                        50.000  strings (     0.000  retained)
ams        eager           
                        11.234M memsize (   838.450k retained)
                       124.150k objects (    10.020k retained)
                        50.000  strings (    50.000  retained)
jsonapi-rb eager           
                         3.993M memsize (     0.000  retained)
                        38.401k objects (     0.000  retained)
                        50.000  strings (     0.000  retained)

Comparison:
jsonapi-rb eager           :    3993316 allocated
jsonapi-rb                 :    4248772 allocated - 1.06x more
ams        eager           :   11234296 allocated - 2.81x more
ams                        :   11384920 allocated - 2.85x more

@dijonkitchen
Copy link

Any updates on this after the release of https://github.com/Netflix/fast_jsonapi ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment