简体   繁体   中英

Javascript large array performance

I am generating randomized string sequences from a 4 different arrays containing up to 700 strings each and now I'm worrying about the performance.

Are there any easy ways to improve perfomance of a static site like this, where you have single jquery click listener and it's randomly creating sentences based on the strings

    var first = [700_ITEMS]
    var sec = [400_ITEMS]
    $(document).ready(function() {
        $('#generate button').click(function(){
            $('#slide1 h1').html(first[Math.floor(Math.random() * first.length)]+" "+sec[Math.floor(Math.random() * sec.length)]);
        })
    });

Maybe I can read the strings somehow on the fly?

Lookup is done once using the index, there's no performance issue unless you are doing something else to populate arrays.

The only possible improvement would be to prerender content but it kind of defeats the purpose of the algorithm.

There isn't much of a performance issue here, at all. You're not calculating all possible permutations between both arrays. It's a simple lookup to find the values, once both random numbers have been found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM