Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
Chrome 81
Mac OS X 10.15.4
Desktop
4 years ago
Test name Executions per second
1 1043310.8 Ops/sec
2 5047337.5 Ops/sec
3 6420704.5 Ops/sec
4 8077193.0 Ops/sec
5 4858749.5 Ops/sec
6 4704867.5 Ops/sec
7 3363681.5 Ops/sec
8 8104099.0 Ops/sec
9 1706911.6 Ops/sec
10 9190811.0 Ops/sec
Script Preparation code:
x
 
function chunk1(xs, n) {
  return xs.reduce(function(o, x, i) {
    o[Math.floor(i / n)].push(x);
    return o;
  }, Array(Math.ceil(xs.length / n)).fill([]));
}
function chunk2(xs, n) {
  return (xs.length <= n) ? [xs] : [
    xs.slice(0, n),
    ...chunk2(xs.slice(n), n)
  ]
}
function chunk3(array, size) {
  //declaring variable 'chunked' as an empty array
  let chunked = []
  //looping through the array until it has been entirely "manipulated" or split into our subarrays
  while (array.length > 0) {
    //taking the spliced segments completely out of our original array
    //pushing these subarrays into our new "chunked" array
    chunked.push(array.splice(0, size))
  }
  //returning the new array of subarrays
  return chunked
}
function chunk4(array, size) {
  //declaring variable 'chunked' as an empty array
  let chunked = []
  //setting our start point for our while loop at index 0
  let i = 0;
  //looping through the array until we have reached the final index
  while (i < array.length) {
    //push the sliced subarray of length 'size' into our new 'chunked' array
    chunked.push(array.slice(i, i + size))
    //increment by the size as to avoid duplicates
    i += size;
  }
  //return the array of subarrays
  return chunked
}
function chunk5(array, size) {
  //declaring variable 'chunked' as an empty array
  let chunked = [];
  //for loop iterating through every element of our input array
  for (let ele of array) {
    //declaring variable 'last' as the last index of our 'chunked' array
    const last = chunked[chunked.length - 1];
    //checking if last is undefined or if the last subarray is equal to the size
    if (!last || last.length === size) {
      //then we push the element to be a new subarray in 'chunked'
      chunked.push([ele])
    } else {
      //if not, then we add the element to the 'last' subarray
      last.push(ele)
    }
  }
  //return the array of subarrays
  return chunked
}
function chunk6(array, size) {
  const chunked_arr = [];
  for (let i = 0; i < array.length; i++) {
    const last = chunked_arr[chunked_arr.length - 1];
    if (!last || last.length === size) {
      chunked_arr.push([array[i]]);
    } else {
      last.push(array[i]);
    }
  }
  return chunked_arr;
}
function chunk7(array, size) {
  const chunked_arr = [];
  let copied = [...array]; // ES6 destructuring
  const numOfChild = Math.ceil(copied.length / size); // Round up to the nearest integer
  for (let i = 0; i < numOfChild; i++) {
    chunked_arr.push(copied.splice(0, size));
  }
  return chunked_arr;
}
function chunk8(array, size) {
  const chunked_arr = [];
  let index = 0;
  while (index < array.length) {
    chunked_arr.push(array.slice(index, size + index));
    index += size;
  }
  return chunked_arr;
}
function chunk9(array, size) {
  if (!array) return [];
  const firstChunk = array.slice(0, size); // create the first chunk of the given array
  if (!firstChunk.length) {
    return array; // this is the base case to terminal the recursive
  }
  return [firstChunk].concat(chunk9(array.slice(size, array.length), size));
}
function chunk10(arr, n) {
  let chunked = arr.reduce((chunk, val) => {
    if (chunk[chunk.length - 1].length === n)
      chunk.push([]);
    chunk[chunk.length - 1].push(val);
    return chunk;
  }, [
    []
  ]);
  return chunked
}
Tests:
  • 1

     
    chunk1([1, 2, 3, 4], 2);
  • 2

     
    chunk2([1, 2, 3, 4], 2);
  • 3

     
    chunk3([1, 2, 3, 4], 2);
  • 4

     
    chunk4([1, 2, 3, 4], 2);
  • 5

     
    chunk5([1, 2, 3, 4], 2);
  • 6

     
    chunk6([1, 2, 3, 4], 2);
  • 7

     
    chunk7([1, 2, 3, 4], 2);
  • 8

     
    chunk8([1, 2, 3, 4], 2);
  • 9

     
    chunk9([1, 2, 3, 4], 2);
  • 10

     
    chunk10([1, 2, 3, 4], 2);