We are executing very simple Javascripts using Java for certain validation. The Javascript engine is Nashorn. Using a single thread and max memory of 1GB, we were able to run 40,000 script executions per minute. We can achieve more speed by running multiple threads but the target machines where the program will eventually be running may not be multi-core machines.
As suggested, posting snippet of the code.
public class RuleExecutor {
private ScriptEngineManager manager;
private ScriptEngine engine;
public RuleExecutor() {
this.manager = new ScriptEngineManager();
this.engine = manager.getEngineByName("JavaScript");
}
public Object execute(String[] inputRow, Rule rule) {
Object output = new String("");
Bindings bindings = engine.createBindings();
//code to bind the variables
...
......
bindings.put(.....)
engine.eval(rule.getExpr(), bindings);
output = bindings.get(rule.getTarget().trim());
return output;
}
}
Only one instance of the above class exists throughout the program. The method execute() is called multiple times.
Used JDK 8 (build 1.8.0_101-b13)
How to speed it up?