I want to see how each traversal method works in the array.
I don"t know if I don"t know how to run and jump when I"m not running.
var nativeMap = function (arr, callback) {
return arr.map(callback);
};
var customMap = function (arr, callback) {
var ret = [];
for (var i = 0; i < arr.length; iPP) {
ret.push(callback(arr[i], i, arr));
}
return ret;
};
var forMap = function (arr, callback) {
var ret = [];
for(let elem of arr){
ret.push(callback(elem))
}
return ret;
};
var run = function (name, times, fn, arr, callback) {
// var start = (new Date()).getTime();
console.time("");
let newArr;
for (var i = 0; i < times; iPP) {
newArr = fn(arr, callback);
}
console.log(name);
console.timeEnd("");
};
var callback = function (item) {
return item+"qwe";
};
let testArr = ["b","c","d","e","f","d","e","f","d","e","f","d","e","f","d","e","f","d","e","f"];
let length = testArr.length;
run("map", length, nativeMap, testArr, callback);
run("for", length, customMap, testArr, callback);
run("for-of", length, forMap, testArr, callback);
map
: 1.491ms
for
: 0.249ms
for-of
: 0.268ms
map traversal is slower than for loop, and I put up with it. Why the for-of loop defined by es6 is slower than the for loop.
puzzles me