#3784 closed enhancement (wontfix)
$.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)
Change History (8)
comment:1 Changed 14 years ago by
Cc: | diogobaeder added |
---|---|
Resolution: | → wontfix |
Status: | new → closed |
comment:2 Changed 14 years ago by
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;
};
comment:3 Changed 14 years ago by
Resolution: | wontfix |
---|---|
Status: | closed → reopened |
Changed 14 years ago by
Attachment: | grep[6053].diff added |
---|
comment:4 Changed 14 years ago by
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.
comment:5 Changed 14 years ago by
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!
comment:6 Changed 14 years ago by
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!
comment:7 Changed 13 years ago by
Milestone: | 1.3 → 1.4 |
---|---|
Resolution: | → wontfix |
Status: | reopened → closed |
Version: | 1.2.6 → 1.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.
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.