Splitting array in sub-arrays

JavaScript performance comparison

Test case created by Miguel Molina

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = [];
  for (var i = 0; i< 1000; i++) {
     arr.push(i);
  }
  
  function splitArrayReduce(items, subArrLength) {
      return items.reduce((x, y, i) => {
          if (i % subArrLength === 0) {
              x.push([y]);
          } else {
              x.slice(-1)[0].push(y);
          }
          return x;
      }, []);
  };
  
  function splitArrayWhile1(items, subArrLength) {
      var arr = [];
      var asdf = null;
      do {
          asdf = arr.length * subArrLength;
          arr.push(items.slice(asdf, asdf + subArrLength));
      } while (arr.slice(-1)[0].length === subArrLength);
      return arr;
  };
  
  function splitArrayWhile2(items, subArrLength) {
      var arr = [];
      var asdf = null;
      var end;
      do {
          asdf = arr.length * subArrLength;
          end = asdf + subArrLength;
          arr.push(items.slice(asdf, end));
      } while (items.length > end);
      return arr;
  };
  
  function doPaellaMadrilenya(items, subsections) {
      if (subsections <= 0) return;
      if (subsections >= items.length) return [items];
  
      return items.reduce((a,b) => {
          let group = a[a.length - 1];
          if (!group || group.length >= subsections) {
              a.push([b]);
          } else {
              group.push(b);
          }
          return a;
      }, []);
  }
  
  function doPaellaRecursive(items, subArrLength, start = 0, end = start + subArrLength) {
      const chorizo = items.slice(start, end);
      return items.length > end
          ? [ chorizo, ...doPaellaRecursive(items, subArrLength, start + subArrLength) ]
          : [ chorizo ];
  }
  
  function doPaellaValenciana(items, subsections) {
    var paella = [];
    var n = 0;
    while (n < arr.length+subsections) {
      if (n < arr.length) {
          paella.push(arr.slice(n, n+subsections));
      }
      n += subsections;
    }
    return paella;
  };
  
  function splitArrayWhileManual(items, subArrLength) {
      const desiredLength = items.length / subArrLength;
      const arr = [];
      let arrLength = arr.length;
      while (arrLength < desiredLength) {
          let start = arrLength * subArrLength;
          let end = start + subArrLength;
          const arr2 = arr[arrLength] = [];
          while (start < end && items[start]) {
              arr2[arr2.length] = items[start];
              start++;
          }
          arrLength = arr.length;
      };
      return arr;
  };
  
  function splitArrayDoWhileManual(items, subArrLength) {
      const itemsLength = items.length;
      const arr = [];
      let start = arr.length * subArrLength;
      let end = start + subArrLength;
      do {
          start = arr.length * subArrLength;
          end = start + subArrLength;
          const arr2 = [];
          while (start < end && items[start]) {
              arr2[arr2.length] = items[start];
              start++;
          }
          arr[arr.length] = arr2;
      } while (itemsLength > end);
      return arr;
  };
  
  function doPaellaValencianaWithRomero(items, subsections) {
      var paella = [];
      var n = 0;
      var n2 = 0;
      while (n < arr.length+subsections) {
          if (n < arr.length) {
              paella[n2] = arr.slice(n, n+subsections);
          }
          n += subsections;
          n2 += 1;
      }
      return paella;
  };
  
  function splitArrayKvothe(array, num) {
      let result = [];
      for (let i = 0, l = array.length; i < l; i += num) {
          result.push([array.slice(i, i + num)]);
      }
      return result;
  };
  
  function splitArraySplice(array, num) {
      var arrCopy = array.slice(0);
      let a = [];
      while (arrCopy.length > 0) {
          a.push(arrCopy.splice(0, num));
      }
      return a;
  };
  

};
</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
splitArrayReduce
splitArrayReduce(arr, 5);
pending…
splitArrayWhile1
splitArrayWhile1(arr, 5);
pending…
splitArrayWhile2
splitArrayWhile2(arr, 5);
pending…
doPaellaMadrilenya
doPaellaMadrilenya(arr, 5);
pending…
doPaellaRecursive
doPaellaRecursive(arr, 5);
pending…
doPaellaValenciana
doPaellaValenciana(arr, 5);
pending…
doPaellaValencianaWithRomero
doPaellaValencianaWithRomero(arr, 5);
pending…
splitArrayWhileManual
splitArrayWhileManual(arr, 5);
pending…
splitArrayDoWhileManual
splitArrayDoWhileManual(arr, 5);
pending…
splitArrayKvothe
splitArrayKvothe(arr, 5);
pending…
spliceArraySpliceDestructor
splitArraySplice(arr, 5);
pending…

Revisions

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

0 Comments