1 var bindCallback = require('../internal/bindCallback');
3 /** Native method references. */
4 var floor = Math.floor;
6 /* Native method references for those with the same name as other `lodash` methods. */
7 var nativeIsFinite = global.isFinite,
10 /** Used as references for the maximum length and index of an array. */
11 var MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1;
14 * Invokes the iteratee function `n` times, returning an array of the results
15 * of each invocation. The `iteratee` is bound to `thisArg` and invoked with
16 * one argument; (index).
21 * @param {number} n The number of times to invoke `iteratee`.
22 * @param {Function} [iteratee=_.identity] The function invoked per iteration.
23 * @param {*} [thisArg] The `this` binding of `iteratee`.
24 * @returns {Array} Returns the array of results.
27 * var diceRolls = _.times(3, _.partial(_.random, 1, 6, false));
30 * _.times(3, function(n) {
33 * // => invokes `mage.castSpell(n)` three times with `n` of `0`, `1`, and `2`
35 * _.times(3, function(n) {
38 * // => also invokes `mage.castSpell(n)` three times
40 function times(n, iteratee, thisArg) {
43 // Exit early to avoid a JSC JIT bug in Safari 8
44 // where `Array(0)` is treated as `Array(1)`.
45 if (n < 1 || !nativeIsFinite(n)) {
49 result = Array(nativeMin(n, MAX_ARRAY_LENGTH));
51 iteratee = bindCallback(iteratee, thisArg, 1);
53 if (index < MAX_ARRAY_LENGTH) {
54 result[index] = iteratee(index);
62 module.exports = times;