Reputation: 107
Sample Program
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
System.out.println(jsEngine.eval("a>10 || b<10 || c=10"));
In my program i am using the above code to evaluate 1000's of expressions which uses javascript functions like indexof(),replace,date functions and many more.This kind of expressions used to be evaluated in rhino(when platform java version was java 7) very faster than nashorn.... i should say atleast 15~20 times faster....
Using rhino with Java 8 seems to be a tedious job...can someone suggest the workaround to make it faster...
Upvotes: 1
Views: 1681
Reputation: 4595
Rhino has an interpreter, Nashorn compiles every expression to JVM bytecode, and then internally loads and runs the so generated JVM classes. So every eval will, in addition to parsing, incur bytecode generation and JVM class loading cost.
One thing you can do is cast the script engine to a Compilable, and use it to make a CompiledScript
for repeated invocation.
That won't help if you have many once-evaluated expressions, but it'll help with often-evaluated ones.
Upvotes: 7