RemoveObject from Array (pop vs splice)

JavaScript performance comparison

Test case created

Info

Remove Object from Array test uses pop and splice

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var ArrayRemoveObjectWith_pop = function (arr, delObj) {
        var length = arr.length, oldObj;
        for (var i = 0; i < length; i++) {
            if (arr[i] == delObj) {
                oldObj = arr.pop();
                if(i != length-1){
                  arr[i] = oldObj;
                }
                break;
            }
        }
    };
   
    var ArrayRemoveObjectWith_splice = function (arr, delObj) {
        var length = arr.length, oldObj;
        for (var i = 0; i < length; i++) {
            if (arr[i] == delObj) {
                arr.splice(i, 1);
                break;
            }
        }
    };
   
    var arr = [];
    for(var i=0; i < 1000; i++) {
      arr[i] = i;
    }
   
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
pop
ArrayRemoveObjectWith_pop(arr , 500);
ArrayRemoveObjectWith_pop(arr , 10);
ArrayRemoveObjectWith_pop(arr , 888);
ArrayRemoveObjectWith_pop(arr , 0);
ArrayRemoveObjectWith_pop(arr , 999);
pending…
splice
ArrayRemoveObjectWith_splice(arr , 500);
ArrayRemoveObjectWith_splice(arr , 10);
ArrayRemoveObjectWith_splice(arr , 888);
ArrayRemoveObjectWith_splice(arr , 0);
ArrayRemoveObjectWith_splice(arr , 999);
pending…

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

Compare results of other browsers

0 comments

Add a comment