Commit f68bbd6
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.083350s1 parent b93b23d commit f68bbd6
1 file changed
Lines changed: 6 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
248 | 248 | | |
249 | 249 | | |
250 | 250 | | |
251 | | - | |
| 251 | + | |
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
| |||
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
280 | | - | |
| 280 | + | |
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
| |||
349 | 349 | | |
350 | 350 | | |
351 | 351 | | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
| 352 | + | |
| 353 | + | |
356 | 354 | | |
357 | 355 | | |
358 | 356 | | |
359 | | - | |
| 357 | + | |
360 | 358 | | |
361 | 359 | | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
| 360 | + | |
367 | 361 | | |
368 | 362 | | |
369 | 363 | | |
| |||
0 commit comments