Editing map This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Why is map so much slower then a while loop? We can compare Array.prototype.map the [specification](http://es5.github.com/#x15.4.4.19) [as implemented here on mdn](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map)(judging by IE9 performance I don't think it's an accurate implementation for all browsers) and a quickMap function that just uses a basic while loop. If we play around with specification map we can see that checking if the key is there (i.e. treating it like a sparse array) is the biggest slowdown in chrome, doubling the speed, the other big bottle neck is using function.call every time. If we only use function.call when thisArg is defined, and don't bother to check if all keys are there then we can boost our speed back up to quickMap speeds. Yay!, except the spec is pretty clear that this needs to be undefined if it isn't specified Ok so we can create a version that binds it once right to avoid creating the new closure so meany times right ? _WRONG!_ Chrome hates bind. Best I could come up with that didn't trash the speak was to create an empty object and call the function as a method of that, not spec but close. Next up if it is called on a sparse array the function should only be called on defined keys, why don't we just use object keys to grab the keys that exist? _WRONG!_ just checking once slows down Chrome somewhat and hoses IE9. I can't think of another way to check for array sparseness, and as we can also see checking if a key is defined doesn't make a difference. So it seems to me if your not using this and not using a sparse array your better off rolling your own map. Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) var _len = 10000; var a = new Array(_len); for (var i = 0; i < _len; i++) { a[i] = i; } var f = function(a) { return a * a; } 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; }; Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var out = a.map(f); Test 2 Title Async (check if this is an asynchronous test) Code var out = a.qMap(f); Test 3 Title Async (check if this is an asynchronous test) Code var out = a.sMap(f); Test 4 Title Async (check if this is an asynchronous test) Code var out = a.cinMap(f); Test 5 Title Async (check if this is an asynchronous test) Code var out = a.ncMap(f); Test 6 Title Async (check if this is an asynchronous test) Code var out = a.cinncMap(f); Test 7 Title Async (check if this is an asynchronous test) Code var out = a.ncObjLitMap(f); Test 8 Title Async (check if this is an asynchronous test) Code var out = a.bMap(f); Test 9 Title Async (check if this is an asynchronous test) Code var out = a.idMap(f); Test 10 Title Async (check if this is an asynchronous test) Code var out = a.okMap(f); Test 11 Title Async (check if this is an asynchronous test) Code var out = a.ocisMap(f);