Skip to content

Commit

Permalink
Use array.indexOf if available (speeds up modern browsers). Thanks to…
Browse files Browse the repository at this point in the history
… lrbabe and Andrea for the patch. Fixes #5160.
  • Loading branch information
louisremi authored and jeresig committed Nov 11, 2009
1 parent 5ac2e08 commit c07d15d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core.js
Expand Up @@ -441,6 +441,10 @@ jQuery.extend({
}, },


inArray: function( elem, array ) { inArray: function( elem, array ) {
if ( array.indexOf ) {
return array.indexOf( elem );
}

for ( var i = 0, length = array.length; i < length; i++ ) { for ( var i = 0, length = array.length; i < length; i++ ) {
if ( array[ i ] === elem ) { if ( array[ i ] === elem ) {
return i; return i;
Expand Down

3 comments on commit c07d15d

@louisremi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Andrea Giammachi suggested a larger change which would speed up inArray with other Array-like objects, see
http://groups.google.com/group/jquery-dev/browse_thread/thread/5f23ae0f85bebb89/2b014e7e55cb32c7?lnk=gst&q=indexOf#2b014e7e55cb32c7

@jeresig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of that particular change - it as assumes that all arry indexOf checks actually have the indexOf method (which is not the case for NodeLists, arguments objects, etc.).

@louisremi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, the change proposed by Andrea is in the first message of the thread: http://groups.google.com/group/jquery-dev/browse_thread/thread/5f23ae0f85bebb89/2b014e7e55cb32c7

Please sign in to comment.