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 with 4 additions and 0 deletions.
  1. +4 −0 src/core.js
@@ -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

This comment has been minimized.

Copy link
Contributor Author

louisremi replied Nov 11, 2009

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

This comment has been minimized.

Copy link
Member

jeresig replied Nov 11, 2009

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

This comment has been minimized.

Copy link
Contributor Author

louisremi replied Nov 11, 2009

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.
You can’t perform that action at this time.