JavaScript 犀牛 + JQuery简单脚本性能问题
想问一下今天是否有可能在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 文档构建目录。
我使用以下命令在 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 秒。 我还发现最慢的部分是 Build TOC - 大部分时间大约需要 10 秒。
只是补充一点,输入文件或多或少是一个 23 KB 大小的小文档。
我想知道为什么要跑这么长时间。我希望它能在不到一秒的时间内完成。
问题:这里性能损失的根源是什么? 我可以想象的选项: 1) 犀牛 2) 环境J 3)JQuery 4) 我的脚本
如果有任何其他执行环境的建议,我们将不胜感激。但需要注意的是,它需要集成到跨平台开发周期中。
Wanted to ask if it's possible today to have a good performance for running javascript in standalone mode with Rhino.
I have a performance issue running Rhino, EnvJS + JQuery script.
Here is the script:
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'))
It's a slightly modified version of the script I've found on the web to build a TOC for input html document.
I run it on command line on 3.06Ghz processor using following command:
$ 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
It takes 16 seconds to complete quite a lot for processing.
I've also found that the most slow part is Build TOC - it takes most of the time about 10 seconds.
Just to add that input file is more or less small document 23 kilobytes size.
I wonder why it takes so long to run. I would expect it to complete in less than a second.
Question: what is the source of performance loss here?
Options I can imagine:
1) Rhino
2) EnvJs
3) JQuery
4) My Script
Any suggestions of other execution environments would be quite appreciated. But need to note that it requires to be integrated into cross-platform development cycle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是猜测(不适用于 EnvJs 和控制台 rhino - 仅嵌入)
“解释”模式可能会导致速度的巨大损失。
(在我的应用程序中它是 context.setOptimizationLevel(-1) )
我必须使用它,因为 jQuery 主方法超过了 64K Java 方法大小限制。
rhino 也有“编译”模式 - 速度更快。
Just guess (did not worked with EnvJs and console rhino - only embedded)
A great loss of speed can be caused by "interpreting" mode.
(it is context.setOptimizationLevel(-1) at my app)
I had to use it as jQuery main method exceeds 64K Java method size limit.
rhino have "compiling" mode too - which is faster.