string concat vs array push

Benchmark created on


Setup

let s = '';
const l = 1000000

Test runner

Ready to run.

Testing in
TestOps/sec
string concat
for (let i = 0; i < l; i++) {
	s += String.fromCharCode(i)
}

console.log(s.length)
ready
arr push
const arr = []
for (let i = 0; i < l; i++) {
	arr.push(String.fromCharCode(i))
}

console.log(arr.join('').length)
ready
arr pre-allocation
const arr = Array(l)
for (let i = 0; i < l; i++) {
	arr[i] = String.fromCharCode(i)
}

console.log(arr.join('').length)
ready

Revisions

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