Shift vs splice(0,1) (v2)

Revision 2 of this benchmark created by TimTucker on


Setup

var data = [0,1,2,3,4,5,6,7,8,9];
    
    var shiftFunction = function() {
       data.shift();
    };
    
    var spliceFunction = function() {
       data.splice(0, 1);
    };
    
    var alwaysSplice = function(input, index) {
      if (index >= 0) {
        input.splice(index, 1);
      }
    };
    
    var sometimesShift = function(input, index) {
      if (index > 0) {
        input.splice(index, 1);
      }  
      else if (index === 0) {
         input.shift();
      }
      
    };

Test runner

Ready to run.

Testing in
TestOps/sec
always splice (0)
alwaysSplice(data, 0);
ready
always splice (3)
alwaysSplice(3);
ready
sometimes shift (0)
sometimesShift(0);
ready
sometimes shift (3)
sometimesShift(3);
ready

Revisions

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

  • Revision 2: published by TimTucker on