append string vs array push

Benchmark created on


Description

https://twitter.com/fabiospampinato/status/1692181664972062800

Preparation HTML

<output id="out"></output>

Setup

let n = 0;
function getStr(){
 if( ++n > 1e6 ) return '';
 return String(n);
}

Teardown

n = 0;
document.getElementById('out').value = '';

Test runner

Ready to run.

Testing in
TestOps/sec
String append
let buff = '';
let str = '';
document.getElementById('out').value = '';
while(str = getStr()) buff += str;
document.getElementById('out').value = buff;
ready
Array push
let buff = [];
let str = '';
document.getElementById('out').value = '';
while(str = getStr()) buff.push(str);
document.getElementById('out').value = buff.join('');
ready

Revisions

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