Are there any tips and tricks for making rhino faster?

Are there any tips and tricks for making rhino faster? I am 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 a rhino in the side space of a Java server?

+7
source share
2 answers

With the JavaScript API through 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 be displayed when you run the script several times. This basically reduces the overhead of re-interpreting each time. From CompiledScript javadoc:

Extended classes that store compilation results. State can be stored as Java classes, Java class files, or scripted opcode codes. The script can be executed repeatedly without reprocessing.

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

And there is an implementation of V8 Java. Check out jav8 .

+5
source

v8

or try using compilation mode instead of interpretation mode.

0
source

All Articles