JS Chunking

JavaScript performance comparison

Revision 4 of this test case created by Neil

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
  var size = 2;

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
chunkRecurse
let chunkRecurse = function (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(chunkRecurse(array.slice(size, array.length), size)); 
}

chunkRecurse(array, size);
pending…
chunkFor
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]);
      }
    }
pending…
chunkSlice
const chunked_arr = [];
  let index = 0;
  while (index < array.length) {
    chunked_arr.push(array.slice(index, size + index));
    index += size;
  }
pending…
chunkForChunksWithSlice
const chunked_arr = [];
  let copied = array.slice(0);
  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));
  }
pending…
item in
var output = [],
          i = 0,
          n = 0;

        for (item in array) {

          if (i >= size) {
            i = 0;
            n++;
          }

          if (!output[n] || output[n] == 'undefined') {
            output[n] = [];
          }

          output[n][i] = array[item];

          i++;

        }
pending…
chunkForCacheLength
const chunked_arr = [];
    for (let i = 0,len=array.length; i < len; i++) {
      const last = chunked_arr[chunked_arr.length - 1];
      if (!last || last.length === size) {
        chunked_arr.push([array[i]]);
      } else {
        last.push(array[i]);
      }
    }
pending…
chunkForChunks
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));
  }
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments