ArrayMapVsSortVsForVsWhile
JavaScript performance comparison
Info
Array.map - Array.sort - for - While
Preparation code
<script>
Benchmark.prototype.setup = function() {
function mapBySort(func, thisArg){
var thisArr = this;
thisArr.func = func;
thisArg = thisArg || this;
var newArr = [];
if (typeof func == "function"){
var len = thisArr.length;
var index = 0;
var finalVal = thisArr[len - 1];
thisArr.sort(function(a,b){
newArr.push(thisArg.func(a));
return 0;
});
newArr.push(thisArg.func(finalVal));
}
return newArr;
};
function mozillaMap(callback, thisArg) {
var T, A, k;
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) {
T = thisArg;
}
A = new Array(len);
k = 0;
while(k < len) {
var kValue, mappedValue;
if (k in O) {
kValue = O[ k ];
mappedValue = callback.call(T, kValue, k, O);
A[ k ] = mappedValue;
}
k++;
}
return A;
};
function mapByMap(func, thisArg){
return this.map(func, thisArg);
}
var mapFunc = function(val){
return val * 10;
}
var someArr = [0,1,2,3,4,5,6,7,8,9];
};
Benchmark.prototype.teardown = function() {
otherArr1 = undefined;
otherArr2 = undefined;
otherArr3 = undefined;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
sort |
|
pending… |
mozilla |
|
pending… |
mapreal |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments