Skip to content
Permalink
Browse files
Use array.indexOf if available (speeds up modern browsers). Thanks to…
… lrbabe and Andrea for the patch. Fixes #5160.
  • Loading branch information
louisremi authored and jeresig committed Nov 11, 2009
1 parent 5ac2e08 commit c07d15d5a26c5b5dc2af7b396a704784d5bede34
Showing 1 changed file with 4 additions and 0 deletions.
@@ -441,6 +441,10 @@ jQuery.extend({
},

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

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

3 comments on commit c07d15d

@louisremi
Copy link
Contributor Author

@louisremi louisremi commented on c07d15d Nov 11, 2009

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

@jeresig jeresig commented on c07d15d Nov 11, 2009

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

@louisremi louisremi commented on c07d15d Nov 11, 2009

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.