我有一个相当大的文件需要经常评估,
与 nashorn 我曾经做过类似的事情:
CompiledScript compiledScript = ((Compilable) engine).compile(text);
后来,我可以多次拨打以下电话:
Context context = new SimpleScriptContext();
compiledScript.eval(context);
这是相当快的。
使用新的 Polyglot API,我这样做:
Source source = Source.newBuilder("js", myFile).build();
然后 :
Context context = Context.newBuilder("js").option("js.nashorn-compat", "true").build();
context.eval(source)
使用jmh,我两者之间的性能差异很大
Benchmark Mode Cnt Score Error Units
JmhBenchmark.testEvalGraal avgt 5 42,855 ± 11,118 ms/op
JmhBenchmark.testEvalNashorn avgt 5 2,739 ± 1,101 ms/op
如果我eval
在相同的上下文中执行此操作,它可以正常工作,但我不想在两个连续的上下文之间共享上下文eval
(除非 Graal 上下文的概念与 Nashorn 的上下文不同)。