Description
Hello,
I'm migrating to GraalVM from Nashorn script engine.
My usage of the engine was running js scripts from within my spring boot app.
The scripts always have an identical structure: (IIFE - immediately invoked function expression)
(function(arg1) { // arg1 is a json
// here we have conditions regarding arg1 fields and in the end we return a value
...
})(arg1)
We also have a global script which contain global functions we want to support.
My initial implementation was through the Context object (which I thought was the recommended way):
Context context = createContext();
Value contextBindings = context.getBindings("js");
contextBindings.putMember("arg1", proxyObject);
context.eval(globalFunctions); // globalFunctions is Source object that is created once on startup.
return context.eval("js", expression);
This code works and return the expected results.
The problem is with the performance.
My test was to run a loop of 50,000 iterations and it took 26574 ms.
I ran with a profiler and I saw to my surprise that the line context.getBindings("js")
takes the most CPU - more than 88%.
Then I ran the same test on my old Nashorn implementation and it took 8897 ms.
Why are the first performance test result so bad?
Why is getBindings call costs so much?
How can I improve my performance?