IE7和IE6下+=的低效问题

话不多说,看代码

<script>
var getTimeInMs() = function () {
      var date = new Date();
      return date.getTime();
}
//使用+=来连接字符串
var str = "I'm a short string after all.",
      newStr = "",
      appends = 15000,
      stime = getTimeInMs();
      while (appends--) {
            newStr += str;
      }
      alert(getTimeInMs() - stime);
//使用Array.prototype.join来连接字符串
var appends = 15000,
      stime = getTimeInMs(),
      arr = [];
      while (appends--) {
            arr.push(str);
      }
      newStr = arr.join("");
      alert(getTimeInMs() - stime);
</script>

在IE8和所有现代浏览器上,第一段代码会快一点,速度在10ms左右,但是在IE6上,第一段代码执行了2秒还多,后一段执行了40ms,当我把循环次数加大到50000的时候,第一段代码执行了42秒,第二段是110ms。

结论:如果你的用户中有IE6用户,而且有大量的字符串拼接,建议使用Array.prototype.join来处理。

这是为什么呢?以下引用一段文字:

IE7 and earlier use an inferior implementation of concatenation in which each pair of concateated strings always be copied to a new memory location. …. IE7’s naive concatenation algorithm requires that the browser repeatedly copy and allocate memory for larger and larger strings each time through the loop. The result is quadratic running time and memory consumption.

A Rule of Thumb When DOM Scripting

原则是:尽可能的不要接触DOM(touch the DOM lightly, and stay within ECMAScript as much as possible).

Here is an example

function aBadInnerHTMLLoop() {
    for (var i = 0; i < 15000; i++) {
        document.getElementById('here').innerHTML += 'a';
    }
}

上面这段代码的效率之低下,令人叹为观止,在我的Chrome下面,执行了4900ms!

function aBetterInnerHTMLLoop() {
    var str = '';
    for (var i = 0; i < 15000; i++) {
        str += 'a';
    }
    document.getElementById('here').innerHTML = str;
}

上面这段代码只执行了4ms。。。

下面我引用一段文献[High Performance JavaScript]来说明为什么DOM的执行如此低下。

DOM in the Browser World
The Document Object Model(DOM) is a language-independent application interface(API) for working with XML and HTML documents. … so it’s common across browsers to keep DOM and JavaScript implementions indepent of each other, this makes DOM scripting inherently slow. Because simply having two separate pieces of functionality interfaceing with eath other will always come at a cost. An excellent analogy is to think of DOM as piece of land and JavaScript as another piece of land, both connected with a toll bridge. Every time your EMACScript needs access to the DOM, you have to cross this bridge and pay the performance toll fee. The more you work with the DOM, the more you pay So the general recommendation is to cross that bridge as few times as possible and strive to stay in ECMAScript land.

Linux在系统中查找执行命令的程序命令which,type

查看某个程序是否可以使用可以用which命令,which就是你问系统如果我要输入一条具体的命令,那么系统会运行哪一个文件,如果系统存在这个命令,它会告诉你这个命令的程序路径,如果没有这个命令,它就告诉你没有这个命令。
[root@localhost ~]# which date
/bin/date
 
[root@localhost ~]# which date less vi emacs
/bin/date
/usr/bin/less
/bin/vi
/usr/bin/which: no emacs in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
 
如果你用的shell是Bash,你还可以使用type命令来代替where,命令如下:
[root@localhost ~]# type date
date is /bin/date

 

Tonitech版权所有 | 转载请注明出处: http://www.tonitech.com/?p=886

解决WordPress“正在执行例行维护,请一分钟后回来”

        在升级WordPress中的插件的时候,半天没有更新完,感觉好像卡了,于是打开首页,发现页面只有提示“正在执行例行维护,请一分钟后回来”。发现,网上很多人说要修改/wp_admin/includes/class-wp-filesystem-direct.php 文件,不过这里有一个快速的解决方案:在FTP 根目录下找到名为 .maintenance的文件,删了就ok,刷新一下页面,一切都正常了。

 

Tonitech版权所有 | 转载请注明出处: http://www.tonitech.com/?p=865