• 2007-12-27

    javascript在不同的浏览器的效能

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://snk.blogbus.com/logs/12800652.html

    javascript在不同的浏览器的效能是不一样的。而且相差很远。

    看看高手们做出的比较吧。

    the SunSpider benchmark: 一个综合易用的 javascript 比较程序网页。

    3dPure JavaScript computations of the kind you might use to do 3d rendering, but without the rendering. This ends up mostly hitting floating point math and array access.
    accessArray, object property and variable access.
    bitopsBitwise operations, these can be useful for various things including games, mathematical computations, and various kinds of encoding/decoding. It's also the only kind of math in JavaScript that is done as integer, not floating point.
    controlflowControl flow constructs (looping, recursion, conditionals). Right now it mostly covers recursion, as the others are pretty well covered by other tests.
    cryptoReal cryptography code, mostly covers bitwise operations and string operations.
    datePerformance of JavaScript's "date" objects.
    mathVarious mathematical type computations.
    regexpRegular expressions. Pretty self-explanatory.
    stringString processing, including code to generate a giant "tagcloud", extracting compressed JS code, etc.

    下面的程序在 FF 不需要 1s,在 IE 需要 30s以上。


    var d = new Date();
    var s = "a";
    for(i=1;i<100000;i++){
    s=s+"a";
    }
    alert(new Date()-d);

     

    同样的效果的代码这个在IE运行的就很快,不需要 1s。


    var d = new Date();
    var a = new Array();
    for(i=1;i<100000;i++){
    a[i]="a";
    }
    var s = a.join("");
    alert(new Date()-d);

    所以大家写 javascript的时候真的要好小心,除了是否能跨浏览器运行,还要考虑写法和效能的问题。


    收藏到:Del.icio.us







    评论

  • 嗨,你的博客怎么这么专业,我看不懂和你有关的任何信息。
    Ben回复黄霞光说:
    哈哈,记录一下工作中碰到的问题。
    2007-12-29 15:20:23