简体   繁体   English

加快此Javasctipt字符串操作的速度

[英]Speeding up this Javasctipt string manipulation

I am changing the content of about 5000 HTML tags and as I read here doing 5000+ html rendering changes is slow and it is better just to redraw the HTML once, 我正在更改大约5000个HTML标记的内容,在此阅读的内容中,进行5000多个html呈现更改很慢,最好只重绘一次HTML,

Therefor I have created a function that loads the entire HTML into a JavaScript string and then goes through the text(looking for a label tag), changes the content of the tags and eventually redraws the HTML once. 为此,我创建了一个函数,该函数将整个HTML加载到JavaScript字符串中,然后遍历文本(查找标签标签),更改标签的内容,并最终重新绘制HTML。

With high hopes, this was also a failure and takes around 30 seconds on a 1000 tags test. 寄予厚望的是,这也是一次失败,并且在进行1000个标签测试时大约需要30秒。

My function basically reverse counts all the visible label DIVs on the screen and adds numbering to them, 我的函数基本上对屏幕上所有可见的标签DIV进行反向计数,并为其添加编号,

Here's the code, what am I doing wrong please? 这是代码,我在做什么错了? (here's an isolated jsfiddle: http://jsfiddle.net/tzvish/eLbTy/8/ ) (这是一个隔离的jsfiddle: http : //jsfiddle.net/tzvish/eLbTy/8/

// Goes through all visible entries on the page and updates their count value
function updateCountLabels()    {
        var entries        = document.getElementsByName('entryWell');
        var entriesId      = document.getElementsByName('entryId');
        var entriesHtml    = document.getElementById('resultContainer').innerHTML;
        var visibleEntries = new Array();
        var countEntries = 0 , pointer = 0;

        // Create a list of all visible arrays:
        for (i = 0; i < entries.length; i++)    {
            if ($(entries[i]).is(":visible")) {
                countEntries++;
                visibleEntries.push(entriesId[i].value);
            }
        }

        // Update html of all visible arrays with numbering:
        for (i = 0; i < visibleEntries.length; i++)            {
            currentId   = visibleEntries[i];

            // Get the position of insertion for that label element:
            elementHtml = '<div id="entryCount'+currentId+'" class="entryCountLabel label label-info">';

            pointer    = entriesHtml.indexOf(elementHtml) + elementHtml.length;
            pointerEnd = entriesHtml.indexOf("</div>",pointer);

            entriesHtml = entriesHtml.substring(0, pointer) + countEntries + entriesHtml.substring(pointerEnd);

            countEntries--;
        }

        // apply the new modified HTML:
        document.getElementById('resultContainer').innerHTML = entriesHtml;
}

From here : 这里

Loops are frequently found to be bottlenecks in JavaScript. 循环经常被发现是JavaScript的瓶颈。 To make a loop the most efficient, reverse the order in which you process the items so that the control condition compares the iterator to zero. 要使循环最有效,请颠倒处理项目的顺序,以便控制条件将迭代器与零进行比较。 This is far faster than comparing a value to a nonzero number and significantly speeds up array processing. 这比将值与非零数字进行比较要快得多,并且可以显着加快阵列处理速度。 If there are a large number of required iterations, you may also want to consider using Duff's Device to speed up execution. 如果需要大量迭代,您可能还需要考虑使用Duff的设备来加快执行速度。

So change: 所以改变:

for (i = 0; i < entries.length; i++) 

To

for (i = entries.length - 1; i ==0 ; i--)

Also notice following paragraph from mentioned link: 另外请注意以下提到链接的段落:

Be careful when using HTMLCollection objects. 使用HTMLCollection对象时要小心。 Each time a property is accessed on one of these objects, it requires a query of the DOM for matching nodes. 每次在这些对象之一上访问属性时,都需要查询DOM以查找匹配的节点。 This is an expensive operation that can be avoided by accessing HTMLCollection properties only when necessary and storing frequently used values (such as the length property) in local variables. 这是一项昂贵的操作,可以通过仅在必要时访问HTMLCollection属性并将经常使用的值(例如length属性)存储在局部变量中来避免。

So change: 所以改变:

for (i = entries.length - 1; i == 0; i--)

To: 至:

var len = entries.length;
for (i = len - 1; i ==0 ; i--)    

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

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