Fast array splice (v27)

Revision 27 of this benchmark created on


Description

Note that the fast splice method only makes sense when order within the array is irrelevant (such as in an object pool.)

Setup

var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

Test runner

Ready to run.

Testing in
TestOps/sec
Regular splice
var i;
for (i = 0; i < 5; i++) {
  arr.splice(i, 1);
}
ready
Fast splice
var i;
for (i = 0; i < 5; i++) {
  var tmp = arr[i],
      len = arr.length;
  arr[i] = arr[len - 1];
  delete arr[len - 1];
}
ready
Sort and Pop
var i;
for (i = 0; i < 5; i++) {
arr.sort( 
    function(a,b){
        if(a === i){ return -1; }//keeps moving the element to the right
    }
);
arr.pop();
}
 
ready
Fast & Low GC
var i, j;
for (i = 0; i < 5; i++) {
  var l = arr.length;
  j = i;
  if (l) {
    while (j < l) {
      arr[j++] = arr[j];
    }
    --arr.length;
  }
}
ready

Revisions

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