Skip to content

Use JSON.generate instead of Yajl.dump #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Watson1978
Copy link

@Watson1978 Watson1978 commented May 27, 2025

What does this PR do?

Recently, Ruby's JSON has huge improvement and it is much faster.
Ref. https://byroot.github.io/ruby/json/2024/12/15/optimizing-ruby-json-part-1.html

So, this PR will change yajl-ruby to Ruby's JSON.

Motivation

Improve performance by using Ruby's JSON

Additional Notes

Please refer following benchmark code and result.

envronment

  • Ruby: 3.4.4
  • OS: Linux 6.14.6-2
  • Compiler: gcc 15.1.1

benchmark

require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'benchmark-ips'
  gem 'json'
  gem 'yajl-ruby'
end

require 'json'
require 'yajl'
require 'yajl/version'

puts "* JSON version: #{JSON::VERSION}"
puts "* Yajl version: #{Yajl::VERSION}"

json = '{"a":"Alpha","b":true,"c":12345,"d":[true,[false,[-123456789,null],3.9676,["Something else.",false],null]],"e":{"zero":null,"one":1,"two":2,"three":[3],"four":[0,1,2,3,4]},"f":null,"h":{"a":{"b":{"c":{"d":{"e":{"f":{"g":null}}}}}}},"i":[[[[[[[null]]]]]]]}'
data = JSON.parse(json)

Benchmark.ips do |x|
  x.report('Yajl.dump') { Yajl.dump(data) }
  x.report('JSON.generate') { JSON.generate(data) }

  x.compare!
end

result

$ ruby tmp.rb
* JSON version: 2.12.2
* Yajl version: 1.4.3
ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-linux]
Warming up --------------------------------------
           Yajl.dump    34.964k i/100ms
       JSON.generate   120.598k i/100ms
Calculating -------------------------------------
           Yajl.dump    345.056k (± 2.8%) i/s    (2.90 μs/i) -      1.748M in   5.070452s
       JSON.generate      1.212M (± 1.6%) i/s  (824.89 ns/i) -      6.150M in   5.074868s

Comparison:
       JSON.generate:  1212286.6 i/s
           Yajl.dump:   345056.0 i/s - 3.51x  slower

Recently, Ruby's JSON has huge improvement and it is much faster.
Ref. https://byroot.github.io/ruby/json/2024/12/15/optimizing-ruby-json-part-1.html

So, this PR will change yajl-ruby to Ruby's JSON.

### envronment
- Ruby: 3.4.4
- OS: Linux 6.14.6-2
- Compiler: gcc 15.1.1

### benchmark

```ruby
require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'benchmark-ips'
  gem 'json'
  gem 'yajl-ruby'
end

require 'json'
require 'yajl'
require 'yajl/version'

puts "* JSON version: #{JSON::VERSION}"
puts "* Yajl version: #{Yajl::VERSION}"

json = '{"a":"Alpha","b":true,"c":12345,"d":[true,[false,[-123456789,null],3.9676,["Something else.",false],null]],"e":{"zero":null,"one":1,"two":2,"three":[3],"four":[0,1,2,3,4]},"f":null,"h":{"a":{"b":{"c":{"d":{"e":{"f":{"g":null}}}}}}},"i":[[[[[[[null]]]]]]]}'
data = JSON.parse(json)

Benchmark.ips do |x|
  x.report('Yajl.dump') { Yajl.dump(data) }
  x.report('JSON.generate') { JSON.generate(data) }

  x.compare!
end
```

### result
```
$ ruby tmp.rb
* JSON version: 2.12.2
* Yajl version: 1.4.3
ruby 3.4.4 (2025-05-14 revision a38531fd3f) +PRISM [x86_64-linux]
Warming up --------------------------------------
           Yajl.dump    34.964k i/100ms
       JSON.generate   120.598k i/100ms
Calculating -------------------------------------
           Yajl.dump    345.056k (± 2.8%) i/s    (2.90 μs/i) -      1.748M in   5.070452s
       JSON.generate      1.212M (± 1.6%) i/s  (824.89 ns/i) -      6.150M in   5.074868s

Comparison:
       JSON.generate:  1212286.6 i/s
           Yajl.dump:   345056.0 i/s - 3.51x  slower
```
@Watson1978 Watson1978 requested a review from a team as a code owner May 27, 2025 02:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant