Skip to content

[performance] set default KeyFormatter to nil #261

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

Merged
merged 1 commit into from
Apr 10, 2015
Merged

Conversation

quake
Copy link
Contributor

@quake quake commented Apr 10, 2015

benchmark code

class TestKey
  def initialize(key_formatter)
    @key_formatter = key_formatter
  end

  def key1(key)
    @key_formatter ? @key_formatter.format(key) : key.to_s
  end

  def key2(key)
    @key_formatter.format(key)
  end
end


a = TestKey.new(nil)
b = TestKey.new(Jbuilder::KeyFormatter.new)

called = 10000000
Benchmark.benchmark do |x|
  keys = (1..100).map{|i| "key#{i}"}
  x.report("without formatter:")   { called.times{ a.key1(keys.sample) } }
  x.report("with formatter:")      { called.times{ b.key2(keys.sample) } }
end

result

without formatter:  1.910000   0.000000   1.910000 (  1.918297)
with formatter:  3.030000   0.010000   3.040000 (  3.047730)

rwz added a commit that referenced this pull request Apr 10, 2015
[performance] set default KeyFormatter to nil
@rwz rwz merged commit 7073a76 into rails:master Apr 10, 2015
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.

2 participants