Ticket #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 |
| Blocking: | Blocked by: |
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
Change History
comment:1 Changed 4 years ago by flesler
- Cc diogobaeder added
- Status changed from new to closed
- Resolution set to wontfix
comment:2 Changed 4 years ago by diogobaeder
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 4 years ago by flesler
- Status changed from closed to reopened
- Resolution wontfix deleted
comment:4 Changed 4 years ago by flesler
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 4 years ago by diogobaeder
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 4 years ago by diogobaeder
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 3 years ago by john
- Status changed from reopened to closed
- Version changed from 1.2.6 to 1.4a1
- Resolution set to wontfix
- Milestone changed from 1.3 to 1.4
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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.


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.