It depends.
That if will always be performed in the already slowest browser ever,
Internet Explorer, plus you are removing the possibility to re-use the
native indexOf prototype with non array objects, such an arguments variable,
a collection of nodes, or a jQuery instance itself.

That is why I am more interested in the first proposal, the one that wraps
indexOf if present but I prefer to remove in any case a superfluous if,
specially if some silly library would like to implement an
Object.prototype.indexOf which behaves differently.

On Thu, Sep 3, 2009 at 7:41 PM, lrbabe <lrb...@gmail.com> wrote:

>
> Or a single line change:
>
> 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;
>        }
>    }
>    return -1;
>  },
>
> The availability of indexOf is tested every time but I don't think it
> makes much difference.
>
> On Sep 3, 7:24 pm, Andrea Giammarchi <andrea.giammar...@gmail.com>
> wrote:
> > Since probably somebody left the Dynamic post where it is, I would like
> to
> > open this new discussion about jQuery.inArray, which is basically an
> indexOf
> > and which is not optimized.
> >
> > I simply quote myself to make things clear, Regards.
> >
> >
> ----------------------------------------------------------------------------------
> >
> > Just a curiosity, are you using indexOf with jQuery instances as well?
> >
> > In this case line 411 of core.js
> >
> >     inArray: Array.prototype.indexOf ?
> >         (function( indexOf ) {
> >             return function( elem, array ) {
> >                 return indexOf.call( array, elem );
> >             }
> >         })( Array.prototype.indexOf ) :
> >         function( elem, array ) {
> >             for ( var i = 0, length = array.length; i < length; i++ ) {
> >                 if ( array[ i ] === elem ) {
> >                     return i;
> >                 }
> >             }
> >
> >             return -1;
> >         }
> >     ,
> >
> > otherwise if this function supposes to work only with array (due to
> explicit
> > name) just this:
> >
> >     inArray: Array.prototype.indexOf ?
> >         function( elem, array ) {
> >             return array.indexOf( elem );
> >         } :
> >         function( elem, array ) {
> >             for ( var i = 0, length = array.length; i < length; i++ ) {
> >                 if ( array[ i ] === elem ) {
> >                     return i;
> >                 }
> >             }
> >             return -1;
> >         }
> >     ,
> >
> > I am sure they'll follow this logic so I would not worry about trunk.
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to