Skip to content

Instantly share code, notes, and snippets.

@czj
Created June 11, 2020 13:01
Show Gist options
  • Save czj/911df1a56f4409dc32bc5529a3f26638 to your computer and use it in GitHub Desktop.
Save czj/911df1a56f4409dc32bc5529a3f26638 to your computer and use it in GitHub Desktop.
Benchmarking Rails' `content_tag(:b)` vs `tag.b`
before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
50_000.times { tag.span("hey") }
after = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Rails.logger.debug %(tag.span("hey") } #{after - before}s)
before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
50_000.times { tag.span { "hey" } }
after = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Rails.logger.debug %(tag.span { "hey" } } #{after - before}s)
before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
50_000.times { content_tag(:span, "hey") }
after = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Rails.logger.debug %(content_tag(:span, "hey") } #{after - before}s)
before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
50_000.times { content_tag(:span) { "hey" } }
after = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Rails.logger.debug %(content_tag(:span) { "hey" } } #{after - before}s)
before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
50_000.times { tag_builder.content_tag_string(:span, "hey", nil) }
after = Process.clock_gettime(Process::CLOCK_MONOTONIC)
Rails.logger.debug %(tag_builder.content_tag_string(:span, "hey", nil) } #{after - before}s)
Rails.logger.debug %(50_000.times { tag.span("hey") } })
Rails.logger.debug(Benchmark.ms { 50_000.times { tag.span("hey") } })
Rails.logger.debug %(50_000.times { tag.span { "hey" } } })
Rails.logger.debug(Benchmark.ms { 50_000.times { tag.span { "hey" } } })
Rails.logger.debug %(50_000.times { content_tag(:span, "hey") } })
Rails.logger.debug(Benchmark.ms { 50_000.times { content_tag(:span, "hey") } })
Rails.logger.debug %(50_000.times { content_tag(:span) { "hey" } } })
Rails.logger.debug(Benchmark.ms { 50_000.times { content_tag(:span) { "hey" } } })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment