Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:20:06+00:00 2026-05-28T06:20:06+00:00

I ran some tests , and the data point that the jQuery inArray() is

  • 0

I ran some tests, and the data point that the jQuery inArray() is much slower than a simple loop.

And array.indexOf() is not even on the tests because I previously did other tests, and it performed even worse.

  • Why it is much slower?
  • Why don’t they use simple loops?
  • Is there something that I am overseeing?

There must be a good reason for not using this:

for(var i=0,len=arr.length,rtn=-1;i<len;i++){
    if(arr[i]==="arritem"){
        rtn=i;
        break;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:20:07+00:00Added an answer on May 28, 2026 at 6:20 am

    If you’re going to test jQuery’s inArray, actually test jQuery’s inArray, and compare apples to apples (calling a function to calling a function — you can write the loop inline in places where performance is hugely critical, but it wouldn’t be your default move, presumably): http://jsperf.com/inarraytest/3

    Preparation HTML:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script>
    

    Preparation code:

    var arr=[0,1,2,3,4,5,6,7,8,9];
    
    function arrayLoop(elem, array, i) {
        var len = array.length;
        i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;
        for ( ; i < len; i++ ) {
            // Skip accessing in sparse arrays
            if ( i in array && array[ i ] === elem ) {
                return i;
            }
        }
        return -1;
    }
    

    Tests:

    // jQuery.inArray last
    var rtn=jQuery.inArray(9,arr);
    
    // arrayLoop last
    var rtn = arrayLoop(9,arr);
    
    // jQuery.inArray middle
    var rtn=jQuery.inArray(4,arr);
    
    // arrayLoop middle
    var rtn = arrayLoop(4,arr);
    
    // jQuery.inArray first
    var rtn=jQuery.inArray(0,arr);
    
    // arrayLoop first
    var rtn = arrayLoop(0,arr);
    

    Results on Chrome (which has indexOf) are that jQuery.inArray is always faster than arrayLoop (in the first pair of test cases, where we’re searching for the last entry, dramatically so).

    Results on IE6 (which doesn’t have indexOf): jQuery.inArray is always faster than arrayLoop, though unsurprisingly not by much (as it has to do essentially the same work) — except, curiously, in the case where we’re searching for the first entry in the array, in which case it’s much faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just been running some simple debug tests against arrays, and noticed that when
I just ran into some code that overuse semicolons, or use semicolon for different
I ran into an issue with some third party software that we use to
UPDATE: I removed some print_r & echo tests I had above the loop and
I ran some code through an automatic translator for C# to VB, and it
I've ran some ALTER scripts on the database [SQL Server Server 2005], and overwrote
I ran across some code recently at work (recreated to be similar to what
I recently ran across some 3rd party C# code which does the following: public
I'm checking out some PHP 5.3.0 features and ran across some code on the
I've been studying C# and ran accross some familiar ground from my old work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.