_.without vs Array.prototype.remove

JavaScript performance comparison

Test case created

Info

Who's faster to delete an element from an array, underscore's without method or implementation of Array remove method ?

Preparation code

<script src="//underscorejs.org/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var arr =  _.range(100000);
    var arr2 = _.range(100000);
    var i = 0;
    var j = 0;
   
    Array.prototype.remove = function (needle) {
        var i = 0;
        while (this[i] !== needle && i < this.length - 1) ++i;
        if (this[i] === needle) {
                this.splice(i, 1);
        }
        else {
                throw new Error(needle + " not found in array " + this);
        }
    };
};
</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
_.without
_.without(arr, i);
++i;
pending…
Array.prototype.remove
arr2.remove(j);
++j;
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. Here’s a list of current revisions for this page:

0 comments

Add a comment