From 7d62bbf74f42ec3b05f0b180a15b850bb48fb155 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 31 May 2012 17:27:49 -0600 Subject: [PATCH] Optimize `_.times`. Former-commit-id: beae48853d0e9be1c3016c11bee86a272964dfa2 --- lodash.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index fef419915..f3e1e8715 100644 --- a/lodash.js +++ b/lodash.js @@ -3034,13 +3034,21 @@ * @example * * _.times(3, function() { genie.grantWish(); }); + * // => calls `genie.grantWish()` 3 times + * + * _.times(3, function() { this.grantWish(); }, genie); + * // => also calls `genie.grantWish()` 3 times */ function times(n, callback, thisArg) { + var index = -1; if (thisArg) { - callback = bind(callback, thisArg); - } - for (var index = 0; index < n; index++) { - callback(index); + while (++index < n) { + callback.call(thisArg, index); + } + } else { + while (++index < n) { + callback(index); + } } }