Internet Explorer Javascript性能问题 [英] Internet Explorer Javascript performance problem

查看:73
本文介绍了Internet Explorer Javascript性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Internet Explorer中的JavaScript性能糟透了.那里没有新闻.但是,有一些提示和技巧可以加快速度.例如,有一个三个 部分 系列.我仍然发现自己无法从中获得不错的表现.也许你们中有些人知道还有什么其他方法可以使速度更快?

我想做的是用Javascript从头开始创建一个中等大小的表.假设有300行,每行10个单元格.我的计算机大约需要5-6秒才能完成此操作.好的,理所当然,这是一个有5年历史的钻机,但这还太多了.这是我的伪代码:

<html>
  <body>
    <script type="text/javascript">
      function MakeTable(parent)
      {
        var i, j;
        var table = document.createElement('table');
        var insertRow = table.insertRow;
        for ( i = 0; i < 300; i++ )
        {
          var row = insertRow(-1);
          for ( j = 0; j < 10; j++ )
          {
            var cell = row.insertCell(-1);
            cell.innerHTML = i + ' -  ' + j;
          }
        }
        parent.appendChild(table);
      }
    </script>
    <div onclick="MakeTable(this);">Click Me!</div>
  </body>
</html>

已添加:嗯,显然字符串串联(使用array.join)是唯一的方法.好吧,难过,当然.希望以适当"的DOM方式进行操作. :)

解决方案

以下是我在寻找答案时发现的一个有趣链接: 该页面使用五个不同的脚本/方法来生成表.
根据他们的测试,使用字符串比使用DOM/Table元素快得多. http://www.quirksmode.org/dom/innerhtml.htm l

JavaScript performance in Internet Explorer sucks. No news there. However there are some tips and tricks to speed it up. For example, there is this three part series. Still I find myself unable to squeeze decent performance out of it. Perhaps some of you have an idea what else to do so it was speedier?

What I want to do is create a medium-size table from scratch in Javascript. Say, 300 rows, 10 cells each. It takes at about 5-6 seconds on my computer to do this. OK, granted, it's a 5 year old rig, but that's still too much. Here's my dummy code:

<html>
  <body>
    <script type="text/javascript">
      function MakeTable(parent)
      {
        var i, j;
        var table = document.createElement('table');
        var insertRow = table.insertRow;
        for ( i = 0; i < 300; i++ )
        {
          var row = insertRow(-1);
          for ( j = 0; j < 10; j++ )
          {
            var cell = row.insertCell(-1);
            cell.innerHTML = i + ' -  ' + j;
          }
        }
        parent.appendChild(table);
      }
    </script>
    <div onclick="MakeTable(this);">Click Me!</div>
  </body>
</html>

Added: Hmm, apparently string-concatenation (with array.join) is the only way to go. Well, sad, of course. Was hoping to do it the "proper" DOM-way. :)

解决方案

Here is an interesting link I found when looking for an answer on this: The page uses five different scripts / methods to generate a table.
According to their tests, using strings is by far faster than using DOM / Table elements. http://www.quirksmode.org/dom/innerhtml.html

这篇关于Internet Explorer Javascript性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆