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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:40:04+00:00 2026-06-09T17:40:04+00:00

I have an autocomplete form where the user can type in a term and

  • 0

I have an autocomplete form where the user can type in a term and it hides all <li> elements that do not contain that term.

I originally looped through all <li> with jQuery’s each and applied .hide() to the ones that did not contain the term. This was WAY too slow.

I found that a faster way is to loop through all <li> and apply class .hidden to all that need to be hidden, and then at the end of the loop do $('.hidden').hide(). This feels kind of hackish though.

A potentially faster way might be to rewrite the CSS rule for the .hidden class using document.styleSheets. Can anyone think of an even better way?

EDIT: Let me clarify something that I’m not sure too many people know about. If you alter the DOM in each iteration of a loop, and that alteration causes the page to be redrawn, that is going to be MUCH slower than “preparing” all your alterations and applying them all at once when the loop is finished.

  • 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-09T17:40:05+00:00Added an answer on June 9, 2026 at 5:40 pm

    Whenever you’re dealing with thousands of items, DOM manipulation will be slow. It’s usually not a good idea to loop through many DOM elements and manipulate each element based on that element’s characteristics, since that involves numerous calls to DOM methods in each iteration. As you’ve seen, it’s really slow.

    A much better approach is to keep your data separate from the DOM. Searching through an array of JS strings is several orders of magnitude faster.

    This might mean loading your dataset as a JSON object. If that’s not an option, you could loop through the <li>s once (on page load), and copy the data into an array.

    Now that your dataset isn’t dependent on DOM elements being present, you can simply replace the entire contents of the <ul> using .html() each time the user types. (This is much faster than JS DOM manipulation because the browser can optimize the DOM changes when you simply change the innerHTML.)

    var dataset = ['term 1', 'term 2', 'something else', ... ];
    
    $('input').keyup(function() {
        var i, o = '', q = $(this).val();
        for (i = 0; i < dataset.length; i++) {
            if (dataset[i].indexOf(q) >= 0) o+='<li>' + dataset[i] + '</li>';
        }
        $('ul').html(o);
    });
    

    As you can see, this is extremely fast.


    Note, however, that if you up it to 10,000 items, performance begins to suffer on the first few keystrokes. This is more related to the number of results being inserted into the DOM than the raw number of items being searched. (As you type more, and there are fewer results to display, performance is fine – even though it’s still searching through all 10,000 items.)

    To avoid this, I’d consider capping the number of results displayed to a reasonable number. (1,000 seems as good as any.) This is autocomplete; no one is really looking through all the results – they’ll continue typing until the resultset is manageable for a human.

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

Sidebar

Related Questions

I have a form that a user can fill out that maps to an
I want to have a text box that the user can type in that
I have a main form and non-modal autocomplete form. How can I prevent the
I have an autocomplete field where user can enter lastname firstname middlename in the
I have recaptcha on my signup form. The user must type something in the
I have a form with a select box where the user can select pre-existing
I have a textbox in a form field that gets populated by the user.
jQuery part: I have a jQuery UI 1.8 Autocomplete form that fetches remote JSON
So I have an autocomplete field where a user can select an existing or
I have autocomplete text box, that is JQuery based. when i record the macro,

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.