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
Check if a loop or length > 0 is more efficient or not for checking if a collection is empty or not
function isEmptyLength(obj) {
return obj.length === 0
}
function isEmptyLoop(obj) {
for (const _ in obj) {
return false
}
return true
}
function isArray(arr) {
return Array.isArray(arr);
}
function newEmpty(obj) {
return isArray(obj) ? isEmptyLength(obj) : isEmptyLoop(obj);
}
function newEmpty2(obj) {
return isArray(obj) ? obj.length === 0 : isEmptyLoop(obj);
}
const short = ["hello", "bye", 1,2,3,4,5,6,7,8,9,10,11, "this is good!"];
const big = [];
for (let i = 0; i <= 100_000; i++) {
big.push(i);
}
Ready to run.
Test | Ops/sec | |
---|---|---|
With length and short array |
| ready |
With length and big array |
| ready |
With loop and short array |
| ready |
With loop and big array |
| ready |
With newempty and short array |
| ready |
With newempty and big array |
| ready |
With newempty2 and short array |
| ready |
With newempty2 and big array |
| ready |
Without function call and short array |
| ready |
Without function call and big array |
| ready |
Check length with if (short) |
| ready |
Check length with if (big) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.