array.filter-vs-jquery-grep

JavaScript performance comparison

Test case created by Amey Sakhadeo

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  var nums = [];
      
      for (var i = 0; i < 1000; i++) {
        nums.push({
          foo: 'aaa',
          bar: Math.floor(Math.random() * 1000)
        });
      }
      nativeFilter = Array.prototype.filter;
  
      condition = function(i) {
        return i.val % 2 === 0;
      };
  
      newGrep = function( elems, callback, inv ) {
  		var validatorWrap = function( elem, index ) {
  				var retVal;
  				retVal = !!callback( elem, index );
  				return inv !== retVal;
  			};
  		inv = !!inv;
  
  		return nativeFilter.call( elems, validatorWrap );
  	};

};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
jQuery.grep
var res = $.grep(nums, condition);
pending…
grep with Array.filter
var res = newGrep(nums, condition)
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.

0 Comments