Skip to main content
1 of 4
Stuart
  • 2.8k
  • 14
  • 20

Simply displaying or hiding an entire list of 100,000 elements takes a couple of seconds - that is probably the main bottleneck. But probably in most cases you will want to display much less than the full list, right? In which case you could do something like this. (fiddle)

var texts = [], maxResults = 3000;
// (then fill texts with the desired items)

document.getElementById('search').onkeyup = function() {
    var mt = [],
        typed = document.getElementById('search').value;
    if (typed == '') mt = texts.slice(0, maxResults);
    else {
        for (var p, t = texts.slice(), i = maxResults; i && (p = t.pop());) {
            if (p.indexOf(typed) > -1) {
                mt.push(p);
                i--;
            }
        }
    }
    document.getElementById('results').innerHTML = 
        '<ul><li>' + mt.join('</li><li>') + '</li></ul>';
}

An alternative with similar effects would be only to update the list when at least 2 or 3 characters have been typed. But then you have to decide what to do when the user backspaces, which could once again result in a massive list having to be displayed.

Stuart
  • 2.8k
  • 14
  • 20