简体   繁体   English

如何在JavaScript中优化长数组

[英]How can I optimize a long array in javascript

I have a javascript code that pretty much searches on a forum post if there is a certain text (a yugioh card, specifically) and replaces it with a special code. 我有一个javascript代码,如果有特定文字(特别是yugioh卡),则可以在论坛帖子上进行大量搜索,并用特殊代码替换。 It gets the list of cards to search for from a variable that is an array, defined at the beginning of the script. 它从脚本开头定义的数组变量中获取要搜索的卡列表。

Obviously this brings problems, as there are over 7000 yugioh cards, and I have to have an italian version and an english version. 显然,这会带来问题,因为有超过7000张yugioh卡,而且我必须有意大利语版本和英语版本。

The way the searching works is it has a for loop, and it executes a regexp search one card at a time. 搜索的工作方式是具有for循环,并且一次执行一张卡的正则表达式搜索。 That means that it loops 7000 times, and Firefox doesn't respond for a few seconds. 这意味着它循环了7000次,并且Firefox几秒钟没有响应。 It also significantly lowers loading times. 它还显着减少了加载时间。

var card_names = [{
        "en": "Primitive Butterfly",
        "ita": "Primitive Butterfly"
    }, {
        "en": "Essha the Frost Vassal",
        "ita": "Essha the Frost Vassal"
    }];
var list = document.getElementsByClassName('post_body');
for (indexa = 0; indexa < card_names.length; ++indexa) {
    var card_name_ita = card_names[indexa].ita;
    var card_name_en = card_names[indexa].en;
    var card_name_ita_regexp = new RegExp(card_name_ita, "gi");
    var card_name_en_regexp = new RegExp(card_name_en, "gi");
    var replaced_first = 0;
    var replaced_second = 0;
    var replaced_third = 0;
    var replaced_forth = 0;
    for (indexb = 0; indexb < list.length; ++indexb) {
        element = list[indexb];
        if (card_name_en !== card_name_ita) {
            element.innerHTML = element.innerHTML.replace(/’/g, "'").replace(card_name_en_regexp, function(token) {
                return '<span style="cursor: help; color: red; text-decoration: underline" class="script-info-carta" title="' + encodeURIComponent(card_name_en) + '">' + card_name_en.replace(/ /gi, ' ') + '</span>'
            });
            element.innerHTML = element.innerHTML.replace(card_name_ita_regexp, function(token) {
                return '<span style="cursor: help; color: red; text-decoration: underline" class="script-info-carta" title="' + encodeURIComponent(card_name_en) + '">' + card_name_ita.replace(/ /gi, ' ') + '</span>'
            });
        }
        if (card_name_en === card_name_ita) {
            element.innerHTML = element.innerHTML.replace(/’/g, "'").replace(card_name_en_regexp, function(token) {
                return '<span style="cursor: help; color: red; text-decoration: underline" class="script-info-carta" title="' + encodeURIComponent(card_name_en) + '">' + card_name_en.replace(/ /gi, ' ') + '</span>'
            });
        }
    }
}

As you can see there are only two cards in the array, but normally there are 7000+. 如您所见,阵列中只有两张卡,但是通常有7000+。 I would like to know how I can make this thing run better. 我想知道如何使这件事运行得更好。 I have an external SQL database with all the cards in it, so I thought maybe there is a way of making a chain: Javascript contacts a php page, which in turn gets the db and returns the array of cards. 我有一个外部SQL数据库,其中包含所有卡,因此我想也许有一种制作链的方法:Javascript与php页面联系,后者依次获取数据库并返回卡数组。 I dont know how I could really do this efficiently, so help is appreciated! 我不知道我如何才能真正有效地做到这一点,因此感谢您的帮助!

Array Chunking 数组分块

    //Copyright 2009 Nicholas C. Zakas. All rights reserved.
    //MIT Licensed
function timedChunk(items, process, context, callback){
    var todo = items.concat();   //create a clone of the original

    setTimeout(function(){

        var start = +new Date();

        do {
             process.call(context, todo.shift());
        } while (todo.length > 0 && (+new Date() - start < 50));

        if (todo.length > 0){
            setTimeout(arguments.callee, 25);
        } else {
            callback(items);
        }
    }, 25);
}

This is the reference site link( http://www.nczonline.net/blog/2009/08/11/timed-array-processing-in-javascript/ ) where this js method is available. 这是此js方法可用的参考站点链接( http://www.nczonline.net/blog/2009/08/11/timed-array-processing-in-javascript/ )。

Also refer this link http://snipplr.com/view/46331/process-large-array/ for timed array processing 另请参阅此链接http://snipplr.com/view/46331/process-large-array/以进行定时数组处理

问题是每次都在循环中创建正则表达式(这是非常昂贵的),并在每次迭代中更改dom树,这是很昂贵的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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