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

2

7

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?

Lucylud answered 6/8, 2011 at 11:50 Comment(0)
H
5

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.

Hopi answered 24/9, 2011 at 5:43 Comment(0)
M
0

v8

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

Metrics answered 23/8, 2011 at 7:20 Comment(1)
is there a java implementation for v8? What are the benefits of switching from interpreting over compiling? How can this be achieved with rhino?Lucylud

© 2022 - 2024 — McMap. All rights reserved.