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
I wanted to check if named functions are better than anonymous functions. Plus other comparisons.
var data = Array.apply(null, Array(10)).map(function(_, i) {
return i;
});
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Polyfill
Array.prototype.map2 = function(fun /*, thisArg */ ) {
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = new Array(len);
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
// NOTE: Absolute correctness would demand Object.defineProperty
// be used. But this method is fairly new, and failure is
// possible only if Object.prototype or Array.prototype
// has a property |i| (very unlikely), so use a less-correct
// but more portable alternative.
if (i in t)
res[i] = fun.call(thisArg, t[i], i, t);
}
return res;
};
// My own simple implementation
Array.prototype.map3 = function(callback /*, thisArg */ ) {
'use strict';
if (typeof callback !== 'function') {
throw new TypeError();
}
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0, len = this.length; i < len; i++) {
this[i] = callback.call(thisArg, this[i], i, this);
};
};
// DANNO
Array.prototype.map4 = function(callback /*, thisArg */ ) {
'use strict';
if (typeof callback !== 'function') {
throw new TypeError();
}
var curr;
for (var i = 0, len = this.length; i < len; i++) {
curr = this[i];
this[i] = callback(curr);
};
};
Ready to run.
Test | Ops/sec | |
---|---|---|
Map anonymous |
| ready |
Map named |
| ready |
for simple |
| ready |
for in |
| ready |
while |
| ready |
Map2 anonymous |
| ready |
Map2 named |
| ready |
Map3 anonymous |
| ready |
Map3 named |
| ready |
Map4 anonymous |
| ready |
Map4 named |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.