@@ -19,15 +19,20 @@ def benchmark_parsing(name, json_output)
19
19
Benchmark . ips do |x |
20
20
x . report ( "json" ) { JSON . parse ( json_output ) } if RUN [ :json ]
21
21
x . report ( "oj" ) { Oj . load ( json_output ) } if RUN [ :oj ]
22
- x . report ( "oj strict" ) { Oj . strict_load ( json_output ) } if RUN [ :oj ]
23
22
x . report ( "Oj::Parser" ) { Oj ::Parser . usual . parse ( json_output ) } if RUN [ :oj ]
24
23
x . report ( "rapidjson" ) { RapidJSON . parse ( json_output ) } if RUN [ :rapidjson ]
25
24
x . compare! ( order : :baseline )
26
25
end
27
26
puts
28
27
end
29
28
29
+ # Oj::Parser is very significanly faster (2.70x) on the nested array benchmark
30
+ # thanks to its stack implementation that saves resizing arrays.
30
31
benchmark_parsing "small nested array" , JSON . dump ( [ [ 1 , 2 , 3 , 4 , 5 ] ] *10 )
32
+
33
+ # Oj::Parser is significanly faster (~1.5x) on the next 4 benchmarks
34
+ # in large part thanks to its string caching.
35
+ # Other than that we're either a bit slower or a bit faster than regular `Oj.load`.
31
36
benchmark_parsing "small hash" , JSON . dump ( { "username" => "jhawthorn" , "id" => 123 , "event" => "wrote json serializer" } )
32
37
33
38
benchmark_parsing "test from oj" , <<JSON
@@ -36,4 +41,9 @@ def benchmark_parsing(name, json_output)
36
41
37
42
benchmark_parsing "twitter.json" , File . read ( "#{ __dir__ } /data/twitter.json" )
38
43
benchmark_parsing "citm_catalog.json" , File . read ( "#{ __dir__ } /data/citm_catalog.json" )
39
- benchmark_parsing "canada.json" , File . read ( "#{ __dir__ } /data/canada.json" )
44
+
45
+ # rapidjson is 8x faster thanks to it's much more performant float parser.
46
+ # Unfortunately, there isn't a lot of existing fast float parsers in pure C,
47
+ # and including C++ is problematic.
48
+ # Aside from that, we're faster than other alternatives here.
49
+ benchmark_parsing "float parsing" , File . read ( "#{ __dir__ } /data/canada.json" )
0 commit comments