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 7913003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:44:29+00:00 2026-06-03T13:44:29+00:00

Suppose that you want to check what input string a user has entered in

  • 0

Suppose that you want to check what input string a user has entered in a form field. Which one would be the fastest way to check this input against a list of possible values?

The following examples use jQuery.

First method: using ||

if (input == "firstValue" || input == "secondValue" || ... ) {
    ...
}

Second method: using inArray()

if ($.inArray(input, array) >= 0) {
    ...
}

Are there any significant differences between these two methods?

  • 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-06-03T13:44:31+00:00Added an answer on June 3, 2026 at 1:44 pm

    You don’t want the fastest but the most readable way. And that’s in_array() (JavaScript: array.indexOf(value) >= 0) for more than 2 or 3 values.

    The performance difference is negligible – while a function call and array creation certainly has some overhead, it doesn’t matter compared to expensive operations such a file access, database access, network access, etc.. So in the end nobody will notice the difference.

    Here’s a short benchmark, each with 1 million iterations:

    5.4829950332642 - in_array, the array is recreated everytime
    2.9785749912262 - in_array, the array is created only once
    0.64996600151062 - isset(), the array way created only once and then the values were turned to keys using array_flip()
    2.0508298873901 - ||
    

    So, the fastest, yet still very readable way is this. Unless you create $arr only once and use it many times, there is no need for this and you can simply stay with in_array().

    $arr = array_flip(array('your', 'list', 'of', 'values'));
    if(isset($arr[$value])) ...
    

    In case you did ask for JavaScript (in this case get rid of those $ prefixes!), the best solution is using Array.indexOf():

    ['a', 'b', 'c'].indexOf(value) >= 0
    

    However, not all browsers already support Array.indexOf(), so you might want to use e.g. the function from Underscore.js:

    _.contains(['a', 'b', 'c'], value)
    

    jQuery also has a function for this:

    $.inArray(value, ['a', 'b', 'c'])
    

    The fastest way would be with an object and the in operator, but the object definition is less readable than the array definition:

    value in {'a':0, 'b':0, 'c':0}
    

    Here’s a JSPerf benchmark for the various solutions: http://jsperf.com/inarray-vs-or – but again, the rather big performance difference is negligible in most cases since you are not going to execute the code millions of times in a loop.

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

Sidebar

Related Questions

I’m trying to make a html5 form that contains one email input, one check
Suppose, I want to have a link or a button that when user click
Suppose you want to show a message to a user who has logged in
Suppose that I have a comments form, and I want to prevent bad users
Ok another question in VHDL. Below is my code. Suppose that I want my
Suppose I have a Python class that I want to add an extra property
Suppose I want to put objects that identify a server into a stl set
Suppose I want to find the longest subsequence such that first half of subsequence
Suppose I want to write an app for Android OS that is not going
Example: Suppose in the following example I want to match strings that do not

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.