array.join vs template string to generate string

Benchmark created on


Setup

const alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const vars = [
	Array.from({length: 10}, () => alpha[Math.floor(Math.random() * 26)]).join(''),
	Array.from({length: 20}, () => alpha[Math.floor(Math.random() * 26)]).join(''),
	Array.from({length: 40}, () => alpha[Math.floor(Math.random() * 26)]).join(''),
	Array.from({length: 80}, () => alpha[Math.floor(Math.random() * 26)]).join('')
];
const getRnd = () => vars[Math.floor(Math.random() * (vars.length - 1))]


Test runner

Ready to run.

Testing in
TestOps/sec
array.join
const jsn = `{
  "v1": "${getRnd()}",
  "v2": "${getRnd()}",
  "v3": "${getRnd()}",
  "v4": "${getRnd()}",
  "v5": "${getRnd()}"
}`

const j = JSON.parse(jsn);
ready
template literal
const arrstr = ['{', 
  '"v1": "', getRnd(), '",',
  '"v2": "', getRnd(), '",',
  '"v3": "', getRnd(), '",',
  '"v4": "', getRnd(), '",',
  '"v5": "', getRnd(), '"',
'}'];

const j = JSON.parse(arrstr.join(''));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.