Remove without copy

Benchmark created by Felipe on


Setup

var a = [0,1,2,3,4,5,6,7,8,9];
    a.splice( 1, 0, 2 );
    a.push( 2 );
    a.push( 2 );

Test runner

Ready to run.

Testing in
TestOps/sec
Felipe
function removeWithoutCopy(arr, item) {
    var i = 0,
      l = arr.length;
    for (i = 0; i < l; i++) {
      if(arr[i] == item){
        arr.splice(i, 1);
        --i;
        --l;
      }
    }
    return arr;
  };

removeWithoutCopy(a, 2)
ready
Allan
function removeWithoutCopy(arr, item) {
    while (arr.indexOf(item) > -1) {
        arr.splice(arr.indexOf(item), 1);
    }
    return arr;
  };

removeWithoutCopy(a, 2)
ready

Revisions

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