Are there any tips & tricks for making rhino perform faster? [closed]

微笑、不失礼 提交于 2019-12-21 00:38:32

问题


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?


回答1:


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();
    }
}

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:

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.

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

And there is a V8 Java implementation. Check jav8.




回答2:


v8

or try to use "compiling" mode instead of "interpreting" mode.



来源:https://stackoverflow.com/questions/6966607/are-there-any-tips-tricks-for-making-rhino-perform-faster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!