是否有任何提示&让犀牛表现更快的技巧? [英] Are there any tips & tricks for making rhino perform faster?

查看:113
本文介绍了是否有任何提示&让犀牛表现更快的技巧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何提示&让犀牛表现更快的技巧?我正在尝试使用Rhino中的uglifyJs压缩大型js文件,这需要一分多钟。在java服务器端空间中你有任何关于rhino的提示或其他选择吗?

Are there any tips & tricks for making rhino perform faster? I'm trying to compress a large js file using uglifyJs in Rhino and it takes more than a minute. Do you have any hints or other alternatives for rhino in java server side space?

推荐答案

使用基于Rhino的JavaScript API,你可以简单地使用 Compilable 界面编译脚本。例如:

With the JavaScript API over Rhino you can simply compile the script using the Compilable interface. For example:

public class CompileScript {
    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager engineManager = new ScriptEngineManager();
        ScriptEngine scriptEngine = engineManager.getEngineByName("js");

        //cast to Compilable engine, this is safe for Rhino
        Compilable c = (Compilable) scriptEngine;    
        CompiledScript script = c.compile("print('Hello World')");    //compile

        script.eval();
    }
}

然而,当运行几个时,这会带来好处倍于剧本。基本上它减少了每次重新解释的开销。从 CompiledScript javadoc:

However the benefits of this will show up when running several times the script. Basically it reduces the overhead of re-interpret every time. From the CompiledScript javadoc:


通过存储编译结果的类进行扩展。状态可以以Java类,Java类文件或脚本语言操作码的形式存储。脚本可以重复执行而无需重新分析。

Extended by classes that store results of compilations. State might be stored in the form of Java classes, Java class files or scripting language opcodes. The script may be executed repeatedly without reparsing.

无论如何,我认为你应该看看Rhino JavaScript编译器。它将JavaScript源转换为Java类文件。

Anyway I think you should take a look at the Rhino JavaScript Compiler. It "translates JavaScript source into Java class files".

还有一个V8 Java实现。查看 jav8

And there is a V8 Java implementation. Check jav8.

这篇关于是否有任何提示&让犀牛表现更快的技巧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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