js string concat

Benchmark created on


Description

via https://jsperf.app/cisila/1/preview no char test + template test

Setup

function randomIntFromInterval(min, max) { // min and max included 
  return Math.floor(Math.random() * (max - min + 1) + min)
}

function makeid(length) {
    let result = '';
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const charactersLength = characters.length;
    let counter = 0;
    while (counter < length) {
      result += characters.charAt(randomIntFromInterval(0, charactersLength));
      counter += 1;
    }
    return result;
}

const tokens = [];
const tokensByChar = [];
let realStr = '';

for(let i=0;i<1000;++i){
	const tok = randomIntFromInterval(0, 100) < 25 ? makeid(1) : makeid(randomIntFromInterval(5, 30));
	tokens.push(tok);
	tokensByChar.push(...tok.split(''));
	realStr += tok;
}

// console.log('realStr', realStr);

Test runner

Ready to run.

Testing in
TestOps/sec
=
let text = '';

for(token of tokens) {
	text = text + token;
}

// console.log(text === realStr);
ready
+=
let text = '';

for(token of tokens) {
	text += token;
}

// console.log(text === realStr);
ready
join
const text = tokens.join('');

// console.log(text === realStr);
ready
concat
let text = '';

for(token of tokens) {
	text = text.concat(token);
}

// console.log(text === realStr);
ready
template
let text = '';

for(token of tokens) {
	text = `${text}${token}`;
}

// console.log(text === realStr);
ready

Revisions

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