split arrays in to subarrays slice vs splice

JavaScript performance comparison

Test case created by Teiem

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
              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]

};

Benchmark.prototype.teardown = function() {
  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]

};
</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
splice
while (testArr.length > 1) {
    const temp =  testArr[testArr.length - 1];
    getEveryX(testArr, temp || 1,  temp)
    testArr.length -= 1;
}
pending…
slice
while (testArr.length > 1) {
    const temp =  testArr[testArr.length - 1];
    getEveryX2(testArr, temp || 1,  temp)
    testArr.length -= 1;
}
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