title: Nashorn inefficiency
body: <p>I'm implementing some performance-sensitive code using Nashorn. I do it like this:</p>
  
  <pre><code>private ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(new String[] { "--no-java" });
  
  String someExpression = "someFunction() + someVariable";
  
  // this compiled script gets cached, caching code omitted
  CompiledScript script = ((Compilable)engine).compile(expr);
  
  MyScriptContext context = new MyScriptContext();
  
  Object output = script.eval(context);
  </code></pre>
  
  <p>At runtime, Nashorn insists on making a lot of necessary calls to MyScriptContext. It insists on calling MyScriptContext.getBindings().put("nashorn.global", anObject) on every call to eval(). It then makes a call to MyScriptContext.getAttribute("someVariable") (which it should) and a call to MyScriptContext.getAttribute("someFunction") (which it should not). </p>
  
  <p>It should not make the call for "someFunction()" because that function was available to it at compile time. "someFunction()" needs to get compiled to bytecode and bound in at compile time, not on every call to eval(). eval() is in a tight loop.</p>
  
  <p>How do I persuade Nashorn to make fewer calls to MyScriptContext?</p>