From d01b713ba28ffc0897f6e046a6a8454c044ceb1f Mon Sep 17 00:00:00 2001 From: kenshin-samourai Date: Wed, 27 Nov 2019 15:55:08 +0100 Subject: [PATCH] optimize splitList() function with splice() function --- lib/util.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/util.js b/lib/util.js index 2e2e1fc..2447255 100644 --- a/lib/util.js +++ b/lib/util.js @@ -101,20 +101,14 @@ class Util { * Splits a list into a list of lists each with maximum length LIMIT */ static splitList(list, limit) { - if (list.length <= limit) { + if (list.length <= limit) return [list] - } else { - const lists = [] - // How many lists to create? - const count = Math.ceil(list.length / limit) - // How many elements per list (max)? - const els = Math.ceil(list.length / count) - - for (let i=0; i < count; i++) { - lists.push(list.slice(i * els, (i+1) * els)) - } - return lists + + const lists = [] + while (list.length) { + lists.push(list.splice(0, limit)) } + return lists } /**