Splitting array in sub-arrays (v5)

Revision 5 of this benchmark created by Cidwel Highwind on


Setup

var arr = [];
  for (var i = 0; i< 1000000; 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 splitArrayWhile(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 doPaella(items, subsections) {
    		var paella = [];
        var n = 0;
        while (n < arr.length / subsections) {
          paella.push(arr.slice(n, n+subsections));
          n += subsections;
        }
        return paella;
    };

Test runner

Ready to run.

Testing in
TestOps/sec
test2
splitArrayWhile(arr ,5);
ready
test
splitArrayReduce(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