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

  • Home
  • SEARCH
  • 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 6012017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:20:36+00:00 2026-05-23T02:20:36+00:00

I have a hidden field on my page that stores space separated list of

  • 0

I have a hidden field on my page that stores space separated list of emails.
I can have maximum 500 emails in that field.

What will be the fastest way to search if a given email already exists in that list?
I need to search multiple emails in a loop

  1. use RegEx to find a match

  2. use indexOf()

  3. convert the list to a
    javascript dictionary and then
    search

If this is an exact duplicate, please let me know the other question.
Thanks

EDIT:
Thanks everyone for your valuable comments and answers.
Basically my user has a list of emails(0-500) in db.
User is presented with his own contact list.
User can then choose one\more emails from his contact list to add to the list.
I want to ensure at client side that he is not adding duplicate emails.
Whole operation is driven by ajax, so jsvascript is required.

  • 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-23T02:20:36+00:00Added an answer on May 23, 2026 at 2:20 am

    Instead of looking for the fastest solution, you first need to make sure that you’re actually having a correct solution. Because there are four cases an e-mail address can appear and a naive search can fail:

    1. Alone: user@example.com
    2. At the begin: user@example.com ...
    3. At the end: ... user@example.com
    4. In between: ... user@example.com ...

    Now let’s analyze each variant:

    1. To allow arbitrary input, you will need to escape the input properly. You can use the following method to do so:

      RegExp.quote = function(str) {
          return str.toString().replace(/(?=[.?*+^$[\]\\(){}-])/g, "\\");
      };
      

      To match all four cases, you can use the following pattern:

      /(?:^|\ )user@example\.com(?![^\ ])/
      

      Thus:

      var inList = new RegExp("(?:^| )" + RegExp.quote(needle) + "(?![^ ])").test(haystack);
      
    2. Using indexOf is a little more complex as you need to check the boundaries manually:

      var pos = haystack.indexOf(needle);
      if (pos != -1 && (pos != 0 && haystack.charAt(pos-1) !== " " || haystack.length < (pos+needle.length) && haystack.charAt(pos+needle.length) !== " ")) {
          pos = -1;
      }
      var inList = pos != -1;
      
    3. This one is rather quite simple:

      var dict = {};
      haystack.match(/[^\ ]+/g).map(function(match) { dict[match] = true; });
      var inList = dict.hasOwnProperty(haystack);
      

    Now to test what variant is the fastest, you can do that at jsPerf.

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

Sidebar

Related Questions

I have a form with a hidden Came from Adwords field that will be
I have a page with 2 forms and a hidden field that is outside
I have a hidden input field on a page <div id=SomeDiv> <input type=text name=ID
I have a hidden input field in my form. I noticed that if that
I have a massive viewstate hidden field that is causing my application to be
I have a field in a CCK node form that is hidden, and has
I have an asp.net aspx page and on that page I have a hidden
I have a ascx page where I am using a hidden field to store
I have a HTML hidden field: <input type=hidden title=aaa value=bbb id=TAB runat=server /> I
I have an hidden input field with a value, <form> <input type=hidden value=product/> <select

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.