Last active
November 30, 2017 04:29
-
-
Save entrptaher/eda34fc881bb53736b11d415c665a02a to your computer and use it in GitHub Desktop.
Test Maps (http://jsbench.github.io/#eda34fc881bb53736b11d415c665a02a) #jsbench #jsperf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Test Maps</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
const arr = [] | |
for (var i = 1; i< 10000; i++){ | |
arr.push(i); | |
} | |
const f = (currentItem) => currentItem * 3; | |
// triple the value of every element in a given array | |
const tripleLoop = (arr) => { | |
let results = [] | |
for (let i = 0; i < arr.length; i++){ | |
results.push(arr[i] * 3) | |
} | |
return results | |
} | |
Array.prototype.qMap = function(fun) { | |
var len = this.length; | |
var out = new Array(len); | |
var i = 0; | |
while (i < len) { | |
out[i] = fun(this[i], i, this); | |
i++; | |
} | |
return out; | |
} | |
Array.prototype.sMap = function(callback, thisArg) { | |
var _this, out, i; | |
if (this == null) { | |
throw new TypeError(" this is null or not defined"); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof callback !== "function") { | |
throw new TypeError(callback + " is not a function"); | |
} | |
if (thisArg) { | |
_this = thisArg; | |
} | |
out = new Array(len); | |
i = 0; | |
while (i < len) { | |
if (i in O) { | |
out[i] = callback.call(_this, O[i], i, O); | |
} | |
i++; | |
} | |
return out; | |
}; | |
Array.prototype.ncMap = function(callback, thisArg) { | |
var _this, out, i; | |
if (this == null) { | |
throw new TypeError(" this is null or not defined"); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof callback !== "function") { | |
throw new TypeError(callback + " is not a function"); | |
} | |
if (thisArg) { | |
_this = thisArg; | |
} | |
out = new Array(len); | |
i = 0; | |
while (i < len) { | |
out[i] = callback.call(_this, O[i], i, O); | |
i++; | |
} | |
return out; | |
}; | |
Array.prototype.cinncMap = function(callback, thisArg) { | |
var _this, out, i; | |
if (this == null) { | |
throw new TypeError(" this is null or not defined"); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof callback !== "function") { | |
throw new TypeError(callback + " is not a function"); | |
} | |
out = new Array(len); | |
i = 0; | |
if (thisArg) { | |
_this = thisArg; | |
while (i < len) { | |
out[i] = callback.call(_this, O[i], i, O); | |
i++; | |
} | |
} else { | |
while (i < len) { | |
out[i] = callback(O[i], i, O); | |
i++; | |
} | |
} | |
return out; | |
}; | |
Array.prototype.cinMap = function(callback, thisArg) { | |
var _this, out, i; | |
if (this == null) { | |
throw new TypeError(" this is null or not defined"); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof callback !== "function") { | |
throw new TypeError(callback + " is not a function"); | |
} | |
out = new Array(len); | |
i = 0; | |
var kValue, mappedValue; | |
if (thisArg) { | |
_this = thisArg; | |
while (i < len) { | |
if (i in O) { | |
out[i] = callback.call(_this, O[i], i, O); | |
} | |
i++; | |
} | |
} else { | |
while (i < len) { | |
if (i in O) { | |
out[i] = callback(O[i], i, O); | |
} | |
i++; | |
} | |
} | |
return out; | |
}; | |
Array.prototype.ncObjLitMap = function(callback, thisArg) { | |
var _this, out, i; | |
if (this == null) { | |
throw new TypeError(" this is null or not defined"); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof callback !== "function") { | |
throw new TypeError(callback + " is not a function"); | |
} | |
if (thisArg) { | |
_this = thisArg; | |
} else { | |
_this = {}; | |
}; | |
_this.callback = callback; | |
out = new Array(len); | |
i = 0; | |
while (i < len) { | |
out[i] = _this.callback(O[i], i, O); | |
i++; | |
} | |
delete _this.callback; | |
return out; | |
}; | |
Array.prototype.bMap = function(cb, thisArg) { | |
var _this, out, i, callback; | |
if (this == null) { | |
throw new TypeError(" this is null or not defined"); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof cb !== "function") { | |
throw new TypeError(callback + " is not a function"); | |
} | |
if (thisArg) { | |
_this = thisArg; | |
} | |
callback = cb.bind(_this); | |
out = new Array(len); | |
i = 0; | |
while (i < len) { | |
out[i] = callback(O[i], i, O); | |
i++; | |
} | |
return out; | |
}; | |
Array.prototype.idMap = function(callback, thisArg) { | |
var _this, out, i; | |
if (this == null) { | |
throw new TypeError(" this is null or not defined"); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof callback !== "function") { | |
throw new TypeError(callback + " is not a function"); | |
} | |
if (thisArg) { | |
_this = thisArg; | |
} | |
out = new Array(len); | |
i = 0; | |
while (i < len) { | |
if (typeof O[i] !== "undefined") { | |
out[i] = callback.call(_this, O[i], i, O); | |
} | |
i++; | |
} | |
return out; | |
}; | |
Array.prototype.okMap = function(callback, thisArg) { | |
var _this, out, i, keys; | |
if (this == null) { | |
throw new TypeError(" this is null or not defined"); | |
} | |
var O = Object(this); | |
var keys = Object.keys(O); | |
var len = O.keys >>> 0; | |
if (typeof callback !== "function") { | |
throw new TypeError(callback + " is not a function"); | |
} | |
out = new Array(len); | |
i = 0; | |
if (thisArg) { | |
_this = thisArg; | |
while (i < len) { | |
out[i] = callback.call(_this, O[keys[i]], keys[i], O); | |
i++; | |
} | |
} else { | |
while (i < len) { | |
out[i] = callback(O[keys[i]], keys[i], O); | |
i++; | |
} | |
} | |
return out; | |
}; | |
Array.prototype.ocisMap = function(callback, thisArg) { | |
var _this, out, i; | |
if (this == null) { | |
throw new TypeError(" this is null or not defined"); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof callback !== "function") { | |
throw new TypeError(callback + " is not a function"); | |
} | |
if (thisArg) { | |
_this = thisArg; | |
} else { | |
_this = {}; | |
}; | |
_this.callback = callback; | |
out = new Array(len); | |
i = 0; | |
if (len === Object.keys(O).length) { | |
while (i < len) { | |
out[i] = _this.callback(O[i], i, O); | |
i++; | |
} | |
} else { | |
while (i < len) { | |
if (i in O) { | |
out[i] = _this.callback(O[i], i, O); | |
} | |
i++; | |
} | |
} | |
delete _this.callback; | |
return out; | |
}; | |
}; | |
suite.add("tripleLoop(arr)", function () { | |
tripleLoop(arr) | |
}); | |
suite.add("arr.map(f)", function () { | |
arr.map(f) | |
}); | |
suite.add("arr.qMap(f)", function () { | |
arr.qMap(f) | |
}); | |
suite.add("arr.cinncMap(f)", function () { | |
arr.cinncMap(f) | |
}); | |
suite.add("arr.ncObjLitMap(f)", function () { | |
arr.ncObjLitMap(f) | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("Test Maps"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment