grep vs pure js

JavaScript performance comparison

Revision 6 of this 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>

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
for (var i = 0; i < me.boards.length; i++) {
  var board = me.boards[i];
  if (board.id == 5) return board;
}
pending…
native Array.filter
return me.boards.filter(function(e) {
  return e.id == 5
});
pending…
simple
for (var i = 0; i < me.boards.length; i++) {
  return me.boards[i].id == 5 ? true : false
}
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