Optimize _.times.

Former-commit-id: beae48853d0e9be1c3016c11bee86a272964dfa2
This commit is contained in:
John-David Dalton
2012-05-31 17:27:49 -06:00
parent 3d8cc32302
commit 7d62bbf74f

View File

@@ -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);
}
}
}