Skip to content

Commit f68bbd6

Browse files
committed
Perf. improvements to Hash#to_json in pure implementation generator:
- Use `.each` instead of `for` for iterating over elements. - Initialize `delim`, `result` vars with initial states instead of appending `state.object_nl` later. - Use faster string interpolation where possible on `Hash#json_transform` instead of appending strings repeatedly. Benchmark: ```ruby hash ={:a => {:a => {:a => {:a => {:a => 1}}}}} require 'benchmark/ips' Benchmark.ips do |x| x.report("to_json ") { hash.to_json} end ``` Before: Calculating ------------------------------------- to_json 4990 i/100ms ------------------------------------------------- to_json 52970.8 (±5.9%) i/s - 264470 in 5.010558s After: Calculating ------------------------------------- to_json 5253 i/100ms ------------------------------------------------- to_json 55998.6 (±5.9%) i/s - 283662 in 5.083350s
1 parent b93b23d commit f68bbd6

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

lib/json/pure/generator.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def configure(opts)
248248
else
249249
raise TypeError, "can't convert #{opts.class} into Hash"
250250
end
251-
for key, value in opts
251+
opts.each do |key, value|
252252
instance_variable_set "@#{key}", value
253253
end
254254
@indent = opts[:indent] if opts.key?(:indent)
@@ -277,7 +277,7 @@ def configure(opts)
277277
# passed to the configure method.
278278
def to_h
279279
result = {}
280-
for iv in instance_variables
280+
instance_variables.each do |iv|
281281
iv = iv.to_s[1..-1]
282282
result[iv.to_sym] = self[iv]
283283
end
@@ -349,21 +349,15 @@ def json_shift(state)
349349
end
350350

351351
def json_transform(state)
352-
delim = ','
353-
delim << state.object_nl
354-
result = '{'
355-
result << state.object_nl
352+
delim = ",#{state.object_nl}"
353+
result = "{#{state.object_nl}"
356354
depth = state.depth += 1
357355
first = true
358356
indent = !state.object_nl.empty?
359-
each { |key,value|
357+
each { |key, value|
360358
result << delim unless first
361359
result << state.indent * depth if indent
362-
result << key.to_s.to_json(state)
363-
result << state.space_before
364-
result << ':'
365-
result << state.space
366-
result << value.to_json(state)
360+
result = "#{result}#{key.to_s.to_json(state)}#{state.space_before}:#{state.space}#{value.to_json(state)}"
367361
first = false
368362
}
369363
depth = state.depth -= 1

0 commit comments

Comments
 (0)