Skip to main content

Bug Tracker

Side navigation

#3784 closed enhancement (wontfix)

Opened January 05, 2009 02:57PM UTC

Closed December 05, 2009 02:26AM UTC

Last modified March 14, 2012 12:32PM UTC

$.grep performance improvement

Reported by: diogobaeder Owned by:
Priority: minor Milestone: 1.4
Component: core Version: 1.4a1
Keywords: grep filter performance Cc: diogobaeder
Blocked by: Blocking:
Description

Hi there,

I'd like to propose a performance improvement to the "static method" jQuery.grep (1 code line and 1 comment line, just that):

$.grep = function(elems, callback, inv) {

Returns using JS 1.6 native code, if appliable

if (Array.prototype.filter && !inv) return elems.filter(callback);

var ret = [];

Go through the array, only saving the items

// that pass the validator function

for ( var i = 0, length = elems.length; i < length; i++ )

if ( !inv != !callback( elems[ i ], i ) )

ret.push( elems[ i ] );

return ret;

};

If the client code doesn't use the "invert" argument, and the browser implements Array.prototype.filter, we can use the native method, since the signatures for this one and the $.grep callback are the same.

The difference - using 1000 iterations, and filtering a 1000 element array in each one -:

  • Old implementation: 929 ms
  • New implementation: 812 ms

Conclusion: not that much of a performance boost, but as it's just 1 line of code, maybe it's worth it... what do you guys think?

Attachments (1)
  • grep[6053].diff (0.7 KB) - added by flesler January 05, 2009 11:06PM UTC.
Change History (7)

Changed January 05, 2009 04:26PM UTC by flesler comment:1

cc: → diogobaeder
resolution: → wontfix
status: newclosed

We use grep for nodelists and arguments arrays too. They don't have filter method. We could check whether they have it first, but I think the gain is too small.

Changed January 05, 2009 06:34PM UTC by diogobaeder comment:2

Well, we could force the context to the non-Array object but with the native method, then:

$.grep = function(elems, callback, inv) {

Returns using JS 1.6 native code, if appliable if (Array.prototype.filter && !inv) return Array.prototype.filter.call(elems, callback);

var ret = [];

Go through the array, only saving the items // that pass the validator function for ( var i = 0, length = elems.length; i < length; i++ )

if ( !inv != !callback( elems[ i ], i ) )

ret.push( elems[ i ] );

return ret;

};

Changed January 05, 2009 11:05PM UTC by flesler comment:3

resolution: wontfix
status: closedreopened

Changed January 05, 2009 11:07PM UTC by flesler comment:4

All tests pass on IE6 and FF2 (I suppose the rest too).

The thing.. how much time is lost on those browser that don't support filter ? is it worth speeding up FF and slow down IE ? probably not.

Changed January 05, 2009 11:29PM UTC by diogobaeder comment:5

Got your point, Ariel... indeed, it's a good question, and it's also more code to download... :-(

Nevertheless, I tested the two implementations, passing invert == true, and here are the results of three test repetitions (the first lines being the original grep, and the second being the new ones):

1138ms

1145ms

1150ms

1221ms

1221ms

1156ms

What do you think? I'm not that sure, but the idea of starting to include native JS implementations from newer versions seem clever to me, considering so many new browser releases...

Thanks!

Changed January 07, 2009 06:54PM UTC by diogobaeder comment:6

One more thing that I thought to be important to consider: As browsers evolve, the implementation of newer versions of JS grow as well (http://www.sitepoint.com/blogs/2009/01/06/ies-decline-makes-cross-browser-more-relevant/), so there's a chance a greater number of JS 1.6-capable or 1.8-capable browser users, compared to IE 6/7 ones, might be using jQuery really soon... Am I wrong?

Thanks!

Changed December 05, 2009 02:26AM UTC by john comment:7

milestone: 1.31.4
resolution: → wontfix
status: reopenedclosed
version: 1.2.61.4a1

Unfortunately using the native methods like that will break the order of arguments that the API currently defines. I don't think this is something that we can easily change.