Skip to main content
added 20 characters in body
Source Link
Stuart
  • 2.8k
  • 14
  • 20

Simply displaying or hiding an entire list of 100,000 elements can take a second or two - 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 results = [],
        typed = document.getElementById('search').value;
    if (typed == '') results = texts.slice(0, maxResults);
    else {
        for (var p, t = texts.slice(), i = maxResults; i && (p = t.pop());) {
            if (p.indexOf(typed) > -1) {
                results.push(p);
                i--;
            }
        }
    }
    else results = texts.slice(0, maxResults);
    document.getElementById('results').innerHTML = 
        '<ul><li>' + results.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.

Simply displaying or hiding an entire list of 100,000 elements can take a second or two - 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 results = [],
        typed = document.getElementById('search').value;
    if (typed == '') results = texts.slice(0, maxResults);
    else {
        for (var p, t = texts.slice(), i = maxResults; i && (p = t.pop());) {
            if (p.indexOf(typed) > -1) {
                results.push(p);
                i--;
            }
        }
    }
    document.getElementById('results').innerHTML = 
        '<ul><li>' + results.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.

Simply displaying or hiding an entire list of 100,000 elements can take a second or two - 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 results = [],
        typed = document.getElementById('search').value;
    if (typed) {
        for (var p, t = texts.slice(), i = maxResults; i && (p = t.pop());) {
            if (p.indexOf(typed) > -1) {
                results.push(p);
                i--;
            }
        }
    }
    else results = texts.slice(0, maxResults);
    document.getElementById('results').innerHTML = 
        '<ul><li>' + results.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.

added 20 characters in body
Source Link
Stuart
  • 2.8k
  • 14
  • 20

Simply displaying or hiding an entire list of 100,000 elements can take a second or two - 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 mtresults = [],
        typed = document.getElementById('search').value;
    if (typed == '') mtresults = texts.slice(0, maxResults);
    else {
        for (var p, t = texts.slice(), i = maxResults; i && (p = t.pop());) {
            if (p.indexOf(typed) > -1) {
                mtresults.push(p);
                i--;
            }
        }
    }
    document.getElementById('results').innerHTML = 
        '<ul><li>' + mtresults.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.

Simply displaying or hiding an entire list of 100,000 elements can take a second or two - 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.

Simply displaying or hiding an entire list of 100,000 elements can take a second or two - 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 results = [],
        typed = document.getElementById('search').value;
    if (typed == '') results = texts.slice(0, maxResults);
    else {
        for (var p, t = texts.slice(), i = maxResults; i && (p = t.pop());) {
            if (p.indexOf(typed) > -1) {
                results.push(p);
                i--;
            }
        }
    }
    document.getElementById('results').innerHTML = 
        '<ul><li>' + results.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.

deleted 1 characters in body
Source Link
Stuart
  • 2.8k
  • 14
  • 20

Simply displaying or hiding an entire list of 100,000 elements takescan take a couple of secondssecond or two - 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.

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.

Simply displaying or hiding an entire list of 100,000 elements can take a second or two - 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.

Source Link
Stuart
  • 2.8k
  • 14
  • 20
Loading