grep vs pure js

JavaScript performance comparison

Test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var me = {boards: [{id: 1}, {id:2}, {id:3}, {id:4}, {id:5}, {id:6}, {id:7}, {id:8}, {id:9}, {id:10}]};
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jQuery
var getBoard1 = function(id) {
        return $.grep(me.boards, function (board) {
            return board.id == id;
        });
    };

getBoard1(5);
pending…
JS
var getBoard2 = function(id) {
        for (var i = 0; i < me.boards.length; i++) {
            var board = me.boards[i];
            if (board.id == id)
                return board;
        }
        return null;
    };

getBoard2(5);
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