jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Saw this code and wondered if it was really faster than a "native" concat.
function cons(x, array) {
var l = array.length;
var a = new Array(l + 1);
a[0] = x;
for(var i=0; i<l; ++i) {
a[i + 1] = array[i];
}
return a;
}
function cons_concat (x, array) {
var a = new Array(array.length + 1);
a[0] = x;
a.concat(array);
return a;
}
function cons_concat2 (x, array) {
return [x].concat(array);
}
function cons_push (x, array) {
var a = new Array(array.length + 1);
a[0] = x;
a.length = 1;
a.push.apply(a, array);
return a;
}
function cons_push2 (x, array) {
var a = [x];
a.push.apply(a, array);
return a;
}
var short = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
var long = [];
for (var i = 0; i < 100; i++) long = long.concat(short);
Ready to run.
Test | Ops/sec | |
---|---|---|
cons, short array |
| ready |
concat, short array |
| ready |
cons, long array |
| ready |
concat, long array |
| ready |
unshift short array |
| ready |
unshift long array |
| ready |
push.apply short array |
| ready |
push.apply long |
| ready |
cons_push2 short |
| ready |
cons_push2 long |
| ready |
cons_concat2 short |
| ready |
cons_concat2 long |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.