Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

[ruby/json] Use JSON.generate instead of Object#to_json#10782

Merged
msmith-techempower merged 1 commit into
TechEmpower:masterfrom
p8:ruby/json-generate
Feb 18, 2026
Merged

[ruby/json] Use JSON.generate instead of Object#to_json#10782
msmith-techempower merged 1 commit into
TechEmpower:masterfrom
p8:ruby/json-generate

Conversation

@p8
Copy link
Copy Markdown
Contributor

@p8 p8 commented Feb 18, 2026

JSON.generate is faster than calling Object#to_json.

require 'benchmark/ips'
require 'json'

Benchmark.ips do |x|
  x.config(warmup: 2, time: 5)

  x.report("to_json symbol") do
    { message: 'Hello, World!' }.to_json
  end

  x.report("to_json string") do
    { 'message' => 'Hello, World!' }.to_json
  end

  x.report("JSON.generate symbol") do
    JSON.generate({ message: 'Hello, World!' })
  end

  x.report("JSON.generate string") do
    JSON.generate({ 'message' => 'Hello, World!' })
  end

  x.compare!
end
ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [arm64-darwin24]
Warming up --------------------------------------
      to_json symbol   330.723k i/100ms
      to_json string   325.411k i/100ms
JSON.generate symbol   490.246k i/100ms
JSON.generate string   495.752k i/100ms
Calculating -------------------------------------
      to_json symbol      3.219M (± 8.2%) i/s  (310.65 ns/i) -     16.205M in   5.097780s
      to_json string      3.250M (± 3.5%) i/s  (307.67 ns/i) -     16.271M in   5.012989s
JSON.generate symbol      4.966M (± 1.5%) i/s  (201.35 ns/i) -     25.003M in   5.035419s
JSON.generate string      4.973M (± 2.1%) i/s  (201.10 ns/i) -     25.283M in   5.086778s

Comparison:
JSON.generate string:  4972636.6 i/s
JSON.generate symbol:  4966478.7 i/s - same-ish: difference falls within error
      to_json string:  3250251.2 i/s - 1.53x  slower
      to_json symbol:  3219106.9 i/s - 1.54x  slower

JSON.generate is faster than calling Object#to_json.

```ruby
require 'benchmark/ips'
require 'json'

Benchmark.ips do |x|
  x.config(warmup: 2, time: 5)

  x.report("to_json symbol") do
    { message: 'Hello, World!' }.to_json
  end

  x.report("to_json string") do
    { 'message' => 'Hello, World!' }.to_json
  end

  x.report("JSON.generate symbol") do
    JSON.generate({ message: 'Hello, World!' })
  end

  x.report("JSON.generate string") do
    JSON.generate({ 'message' => 'Hello, World!' })
  end

  x.compare!
end
```

```
ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [arm64-darwin24]
Warming up --------------------------------------
      to_json symbol   330.723k i/100ms
      to_json string   325.411k i/100ms
JSON.generate symbol   490.246k i/100ms
JSON.generate string   495.752k i/100ms
Calculating -------------------------------------
      to_json symbol      3.219M (± 8.2%) i/s  (310.65 ns/i) -     16.205M in   5.097780s
      to_json string      3.250M (± 3.5%) i/s  (307.67 ns/i) -     16.271M in   5.012989s
JSON.generate symbol      4.966M (± 1.5%) i/s  (201.35 ns/i) -     25.003M in   5.035419s
JSON.generate string      4.973M (± 2.1%) i/s  (201.10 ns/i) -     25.283M in   5.086778s

Comparison:
JSON.generate string:  4972636.6 i/s
JSON.generate symbol:  4966478.7 i/s - same-ish: difference falls within error
      to_json string:  3250251.2 i/s - 1.53x  slower
      to_json symbol:  3219106.9 i/s - 1.54x  slower
```
@msmith-techempower msmith-techempower merged commit 727203b into TechEmpower:master Feb 18, 2026
4 checks passed
@p8 p8 deleted the ruby/json-generate branch February 19, 2026 06:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants