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
Testing if the perf hit for a for loop over Object.keys(..)
is enough to matter as compared to for-in
.
<script>
var hasOwn = Object.prototype.hasOwnProperty;
// pre-ES5 polyfill for Object.keys()
Object.keys=Object.keys||function(o,k,r){r=[];for(k in o)r.hasOwnProperty.call(o,k)&&r.push(k);return r}
function iterate1() {
var v = "", i = 0;
for (i in obj) {
if (hasOwn.call(obj, i)) {
v = obj[i];
}
}
}
function iterate2() {
var v = "", i = 0, ks = Object.keys(obj);
for(var i = 0, len = ks.length; i < len; ++i)
v = obj[ks[i]];
}
function iterate3() {
var v = "", i = 0;
for (i in obj) {
v = obj[i];
}
}
var obj = {}, j = 0;
while(j < 5000) {
obj[j.toString()] = j.toString();
++j;
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
for-in |
| ready |
Object.keys(..) for loop |
| ready |
for-in (no hasown) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.