【问题标题】:Javascript Rhino + JQuery simple script performance issueJavascript Rhino + JQuery 简单脚本性能问题
【发布时间】:2011-08-14 12:14:21
【问题描述】:

想问一下今天是否有可能在 Rhino 的独立模式下运行 javascript 时有良好的性能。

我在运行 Rhino、EnvJS + JQuery 脚本时遇到性能问题。

这是脚本:

load('tools/envjs/env.rhino.js')
window.location = "test.html"
load('tools/jquery.js')

// add TOC div
$('body').append('<div id="toc"></div>');

// Build TOC
$("h1, h2, h3").each(function(i) {
    var current = $(this);
    current.attr("id", "title" + i);

    var pos = current.position().top / $("#content").height() * $(window).height();
    $("#toc").append("<a id='link" + i + "' href='#title" + i +
    "' title='" + current.attr("tagName") + "'>" +
    current.html() + "</a>");

    $("#link" + i).css("top", pos);
});    
Envjs.writeToFile(document.documentElement.outerHTML, Envjs.uri('test-toc.html'))

这是我在网上找到的脚本的略微修改版本,用于为输入 html 文档构建 TOC。

我使用以下命令在 3.06Ghz 处理器的命令行上运行它:

$ time java -jar tools/js.jar -opt -1 tools/make-toc.js 
[  Envjs/1.6 (Rhino; U; Mac OS X x86_64 10.7; en-US; rv:1.7.0.rc2) Resig/20070309 PilotFish/1.2.13  ]

real    0m16.554s
user    0m34.131s
sys 0m1.288s

完成相当多的处理需要 16 秒。 我还发现最慢的部分是构建 TOC - 大部分时间大约需要 10 秒。

只是添加输入文件或多或少是 23 KB 大小的小文档。

我想知道为什么运行需要这么长时间。我希望它会在不到一秒的时间内完成。

问题:这里性能损失的来源是什么? 我能想象的选项: 1) 犀牛 2) 环境 3) jQuery 4) 我的脚本

其他执行环境的任何建议将不胜感激。但需要注意的是,它需要集成到跨平台的开发周期中。

【问题讨论】:

  • 查看nodejs.org,它是 google V8 的封装,速度非常快。

标签: javascript jquery rhino envjs


【解决方案1】:

只是猜测(不适用于 EnvJs 和控制台 rhino - 仅嵌入)

“口译”模式会导致速度大大降低。 (在我的应用中是 context.setOptimizationLevel(-1))

我不得不使用它,因为 jQuery 主方法超过了 64K Java 方法大小限制。

rhino 也有“编译”模式——速度更快。

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多