From 1b842f86ca0be3abfae0322d4702298402cc93f5 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 8 Nov 2014 19:34:37 -0800 Subject: [PATCH] Optimize `_.chunk`. --- lodash.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lodash.js b/lodash.js index dca37449d..948fd1ed3 100644 --- a/lodash.js +++ b/lodash.js @@ -3370,12 +3370,13 @@ * // => [['a', 'b', 'c'], ['d']] */ function chunk(array, size, guard) { + size = (guard || size == null) ? 1 : nativeMax(+size || 1, 1); + var index = 0, length = array ? array.length : 0, resIndex = -1, - result = []; + result = Array(ceil(length / size)); - size = (guard || size == null) ? 1 : nativeMax(+size || 1, 1); while (index < length) { result[++resIndex] = slice(array, index, (index += size)); }