Nashorn via JSR223 - SimpleBindings = slow

A. Sundararajan sundararajan.athijegannathan at oracle.com
Thu Aug 1 21:20:04 PDT 2013


The way nashorn integrates user plugged Bindings is by installing 
__noSuchProperty__ and __noSuchMethod__ special hooks in Nashorn's 
Global script object. But, when you set variable via ScriptEngine.put or 
put to default ENGINE_SCOPE Bindings instance (which is of type 
ScriptObjectMirror), you are actually creating a JS global variable 
durectly. i.e., no __noSuchProperty__ hook needed in that case.

One small adjustment you can do in your script:

var intArray = ints; // gets ints from script "context"'s Bindings - 
either ENGINE_SCOPE or GLOBAL_SCOPE - perhaps SimpleBindings
var filter = 0;var i = intArray.length;while(i--) {    if (+intArray[i] 
 > 500) {        filter++;    }}

The above script avoids fetching "ints" inside loop 100 million times -- 
just fetches "ints" once outside the loop. The above should perform 
similar to your first case.

Hope this helps,
-Sundar

On Friday 02 August 2013 03:22 AM, Rod Nim wrote:
> Hi list,
> I'm using Nashorn via ScriptEngineManager, which constructs me a ScriptEngine with an ENGINE_SCOPE Bindings of type jdk.nashorn.api.scripting.ScriptObjectMirror.
> I put an 100 million random 0 to 1000, int array into that context and iterate it in a loop in JS, which takes 3700ms.
> var filter = 0;var i = ints.length;while(i--) {    if (+ints[i] > 500) {        filter++;    }}
> If I set my own bindings though, via ScriptEngine.setBindings():
> Bindings b = new SimpleBindings();b.put("ints", ints);engine.setBindings(b, ScriptContext.ENGINE_SCOPE);
> The exact same piece of JS code now takes 8600ms to execute. Twice as long. And SimpleBindings is pretty simple, just a delegation to HashMap.
> Is there some trick I'm missing?
> Thanks for the help!
> Rod
>
>
>
>   		 	   		



More information about the nashorn-dev mailing list