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
Compare performance of the following iterative patterns:
AngularJS angular.forEach Native array.forEach Native for key in arr Native for loop
Should be quicker when caching length, also updated latest stable AngularJS! :)
var a = angular, u = {};
a.forEach = function(obj, iterator, context) {
var key, objLen;
if (obj) {
if (a.isFunction(obj)) {
for (key in obj) {
// Need to check if hasOwnProperty exists,
// as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function
if (key !== 'prototype' && key !== 'length' && key !== 'name' && (!obj.hasOwnProperty || obj.hasOwnProperty(key))) {
iterator.call(context, obj[key], key);
}
}
} else if (u.isArray(obj) || u.isArrayLike(obj)) {
objLen = obj.length; key = 0;//cached length is faster!
for (; key < objLen; key++) {
iterator.call(context, obj[key], key);
}
} else if (obj.forEach && obj.forEach !== a.forEach) { //https://github.com/angular/angular.js/commit/36625de0d3ebc1fc091af474d942c6ce16b0a1c0
obj.forEach(iterator, context);
} else {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(context, obj[key], key);
}
}
}
}
return obj;
};
u.isWindow = function(obj) {
return obj && obj.document && obj.location && obj.alert && obj.setInterval;
};
u.isArray = (function() {
if (!a.isFunction(Array.isArray)) {
return function(value) {
return toString.call(value) === '[object Array]';
};
}
return Array.isArray;
})();
u.isArrayLike = function(obj) {
if (obj === null || u.isWindow(obj)) {
return false;
}
var length = obj.length;
if (obj.nodeType === 1 && length) {
return true;
}
return a.isString(obj) || a.isArray(obj) || length === 0 || typeof length === 'number' && length > 0 && (length - 1) in obj;
};
function test7WithFn(arr, fn) {
var i = 0, arrLen = arr.length;
for (; i < arrLen; i++) {
fn(arr[i]);
}
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Native forEach |
| ready |
AngularJS forEach |
| ready |
for key in arr |
| ready |
for loop |
| ready |
for loop, super-cached i and arr length, simpler! |
| ready |
for loop, simple (with var) |
| ready |
for loop, improved super-cached i and arr length, even simpler! |
| ready |
AngularJS forEach with Improved forEach Method |
| ready |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.