Skip to content

Instantly share code, notes, and snippets.

@saurabhnanda
Last active January 1, 2016 14:38
Show Gist options
  • Save saurabhnanda/8158656 to your computer and use it in GitHub Desktop.
Save saurabhnanda/8158656 to your computer and use it in GitHub Desktop.
Simple benchmark to demonstrate what a horrible fuck-up Rails/ActiveSupport is with respect to JSON generation. More details at https://github.com/intridea/multi_json/pull/138#issuecomment-24468223

Let's start with a fresh IRB session, load the json gem (which is implemented in C) and run a base benchmark.

[0] % irb
1.9.3p332 :001 > require 'json'
 => true 
1.9.3p332 :002 > require 'benchmark'
 => true 
1.9.3p332 :003 > Benchmark.measure { 500000.times { {'a' => 1, 'b' => [1, 2, '3']}.to_json } }
 =>   8.730000   0.020000   8.750000 (  8.757620)
 
1.9.3p332 :004 > Benchmark.measure { 500000.times { {'a' => 1, 'b' => [1, 2, '3']}.to_json } }
 =>   8.310000   0.010000   8.320000 (  8.332591)

Takes about 8 seconds to generate a small JSON 5 million times. Now let's look at what happens when we load active_support/json/encoding

1.9.3p332 :005 > require 'active_support/json/encoding'
 => true 
1.9.3p332 :006 > Benchmark.measure { 500000.times { {'a' => 1, 'b' => [1, 2, '3']}.to_json } }
 => 101.110000   0.200000 101.310000 (101.445942)
 
1.9.3p332 :007 > Benchmark.measure { 500000.times { {'a' => 1, 'b' => [1, 2, '3']}.to_json } }
 =>  98.440000   0.180000  98.620000 ( 98.738134)

That's right. It took about 100 seconds to do the same thing. My mind boggles. It's worse than even the pure-Ruby JSON gem, which takes about 30 seconds

[0] % irb
1.9.3p332 :004 > require 'json/pure'
 => true 
1.9.3p332 :005 > require 'benchmark'
 => true 
1.9.3p332 :006 > Benchmark.measure { 500000.times { {'a' => 1, 'b' => [1, 2, '3']}.to_json } }
 =>  32.580000   0.060000  32.640000 ( 32.666449)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment