/** * Extend function * ================ * * Function used to push a bunch of values into an array at once. * * Its strategy is to mutate target array's length then setting the new indices * to be the values to add. * * A benchmark proved that it is faster than the following strategies: * 1) `array.push.apply(array, values)`. * 2) A loop of pushes. * 3) `array = array.concat(values)`, obviously. * * Intuitively, this is correct because when adding a lot of elements, the * chosen strategies does not need to handle the `arguments` object to * execute #.apply's variadicity and because the array know its final length * at the beginning, avoiding potential multiple reallocations of the underlying * contiguous array. Some engines may be able to optimize the loop of push * operations but empirically they don't seem to do so. */ /** * Extends the target array with the given values. * * @param {array} array - Target array. * @param {array} values - Values to add. */ module.exports = function extend(array, values) { var l2 = values.length; if (l2 === 0) return; var l1 = array.length; array.length += l2; for (var i = 0; i < l2; i++) array[l1 + i] = values[i]; };