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
This is an Array#filter
implementation from a recent 140bytes code golf entry. It's faster than native Array#filter
when iterating over large sparse arrays because it doesn't use the length
property to calculate when it should stop iterating.
<script>
Array.prototype.filter2 = function(
a, //a function to test each value of the array against. Truthy values will be put into the new array and falsey values will be excluded from the new array
b, // placeholder
c, // placeholder
d, // placeholder
e // placeholder
) {
c = this; // cache the array
d = []; // array to hold the new values which match the expression
for (e in c) // for each value in the array,
~~e + '' == e && e >= 0 && // coerce the array position and if valid,
a.call(b, c[e], +e, c) && // pass the current value into the expression and if truthy,
d.push(c[e]); // add it to the new array
return d // give back the new array
};
</script>
var sparse_array = []
sparse_array[200] = 'a'
var dense_array = [];
for(var i=0; i<200; i++) {
dense_array[i] = String.fromCharCode(Math.round(Math.random() * 95 + 33))
}
function callback(v) {
return /\w+/.test(v);
}
Ready to run.
Test | Ops/sec | |
---|---|---|
native vs sparse array |
| ready |
custom vs sparse array |
| ready |
native vs dense array |
| ready |
custom vs dense array |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.