File: concat_vs_join.rb

package info (click to toggle)
yard 0.9.38-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,736 kB
  • sloc: ruby: 31,680; javascript: 7,658; makefile: 21
file content (13 lines) | stat: -rw-r--r-- 368 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
# frozen_string_literal: true
require "benchmark"

STR1 = "Hello"
JOIN = "::"
STR2 = "World"

TESTS = 100_000
Benchmark.bmbm do |results|
  results.report("concat") { TESTS.times { "".concat(STR1).concat(JOIN).concat(STR2) } }
  results.report("add   ") { TESTS.times { STR1 + JOIN + STR2 } }
  results.report("join  ") { TESTS.times { [STR1, STR2].join(JOIN) } }
end