split arrays in to subarrays slice vs splice

Benchmark created by Teiem on


Setup

const getEveryX = (arr, x, offset) => {
                  const _arr = [...arr];
                  let _arrArr = [];
  
                  if (offset && offset >= arr.length) {
                      _arrArr.push(_arr.splice(0, offset));
                  }
                  while (_arr.length > x) {
                      _arrArr.push(_arr.splice(0, x));
                  }
                  if (_arr.length) {
                      _arrArr.push(_arr);
                  }
                  return _arrArr
              }
  
              const getEveryX2 = (arr, x, offset) => {
                  let _pointer = 0;
                  const _arrArr = [];
                  
                  if (offset && offset >= arr.length) {
                      _arrArr.push(arr.slice(0, offset));
                  }
                  while (arr.length >= _pointer + x) {
                      _arrArr.push(arr.slice(_pointer, _pointer + x));
                      _pointer += x;
                  }
                  if (arr.length) {
                      _arrArr.push(arr.slice(_pointer, arr.length - 1));
                  }
                  return _arrArr;
              };
  let testArr = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]

Teardown



            testArr = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
splice
while (testArr.length > 1) {
    const temp =  testArr[testArr.length - 1];
    getEveryX(testArr, temp || 1,  temp)
    testArr.length -= 1;
}
ready
slice
while (testArr.length > 1) {
    const temp =  testArr[testArr.length - 1];
    getEveryX2(testArr, temp || 1,  temp)
    testArr.length -= 1;
}
ready

Revisions

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