Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 317
Alex Objelean Avatar asked Aug 06 '11 11:08

Alex Objelean


1 Answers

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.

like image 120
DarkByte Avatar answered Sep 28 '22 14:09

DarkByte