Splitting array in sub-arrays

Benchmark created by Miguel Molina on


Setup

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;
  };

Test runner

Ready to run.

Testing in
TestOps/sec
splitArrayReduce
splitArrayReduce(arr, 5);
ready
splitArrayWhile1
splitArrayWhile1(arr, 5);
ready
splitArrayWhile2
splitArrayWhile2(arr, 5);
ready
doPaellaMadrilenya
doPaellaMadrilenya(arr, 5);
ready
doPaellaRecursive
doPaellaRecursive(arr, 5);
ready
doPaellaValenciana
doPaellaValenciana(arr, 5);
ready
doPaellaValencianaWithRomero
doPaellaValencianaWithRomero(arr, 5);
ready
splitArrayWhileManual
splitArrayWhileManual(arr, 5);
ready
splitArrayDoWhileManual
splitArrayDoWhileManual(arr, 5);
ready
splitArrayKvothe
splitArrayKvothe(arr, 5);
ready
spliceArraySpliceDestructor
splitArraySplice(arr, 5);
ready

Revisions

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

  • Revision 1: published by Miguel Molina on
  • Revision 3: published by Cidwel Highwind on
  • Revision 5: published by Cidwel Highwind on