[ruby] Jsonのライブラリを比較してみた
比較対象は、
- ActiveSupport::JSON
- gemのJSON http://json.rubyforge.org/
- こちら http://rubyforge.org/snippet/detail.php?type=snippet&id=148 で公開されているJsonParser
結果は下に貼付けた。
require "rubygems" require "json" require "activesupport" require "json_parser" json = (1..2000).map{|i| { i.to_s => (i+10)*10, :hoge => "ほげ ふが" }}.to_json Benchmark.bm do |x| x.report { 10.times{ ActiveSupport::JSON.decode(json) } } x.report { 10.times{ JSON.parse(json) } } x.report { 10.times{ JsonParser.new.parse(json) } } end json = { :hoge => "ほげ ふが" }.to_json ActiveSupport::JSON.decode(json) # => {"hoge"=>"\\u307b\\u3052 \\u3075\\u304c"} JSON.parse(json) # => {"hoge"=>"\343\201\273\343\201\222 \343\201\265\343\201\214"} JsonParser.new.parse(json) # => {"hoge"=>"\343\201\273\343\201\222 \343\201\265\343\201\214"} # >> user system total real # >> 1.740000 0.020000 1.760000 ( 1.777468) # >> 0.140000 0.000000 0.140000 ( 0.142605) # >> 3.190000 0.650000 3.840000 ( 3.888802)
見れば分かると思いますが、
ActiveSupport::JSONは日本語の扱いに不具合がある。
JsonParserは、遅い。
JSONは、早くて・日本語もOKなのでgemをSKIPでは利用しようかと思います。