Skip to content

[performance] set default KeyFormatter to nil#261

Merged
rwz merged 1 commit intorails:masterfrom
quake:master
Apr 10, 2015
Merged

[performance] set default KeyFormatter to nil#261
rwz merged 1 commit intorails:masterfrom
quake:master

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