16

I am using the Nashorn JavaScript Engine in Java 11 which works fine except it will be deprecated soon. I have tried GraalVM which I find quite worse as it takes 13-14 seconds to execute a simple expression (e.g. 2+3). Is there any other alternative that I could use or should I try GraalVM with some other approach (if there is any)?

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("javascript");
engine.eval("2+3");
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sohaib Zafar
  • 183
  • 1
  • 1
  • 6
  • 1
    Could you elaborate on how you run your code to make it take 13s? That exact code takes under a second on my machine using GraalVM. – BoriS Oct 01 '19 at 19:03
  • Meta comment. The time it took for the answers to this question to become out-of-date clearly illustrates the problem with "recommendation" Q&A's! – Stephen C Jun 06 '22 at 00:38

2 Answers2

5

Update (June 2022):

As of 2022, it seems J2V8 is also poorly maintained.

Better set your sights on Javet


If someone still finds this question relevant, a viable, still actively maintained alternative to Nashorn/Rhino script engines is the J2V8 binding for the JVM.

Artifacts can be found here (be sure to use this link to get updated releases)

Multi-platform support is builtin via JNI

Though its mechanics are a bit different than those of the ScriptEngine API, performance is (as expected) better. and you don't have to deal with weird side effects like unintended class-loading caused by misuse of the API.

I have successfully used it myself in the past.

Sheinbergon
  • 2,875
  • 1
  • 15
  • 26
3

You can use Rhino as replacement

Rhino still is being maintained as an open-source project and so has a history of community involvement. For some uses, it’s clearly superior because of its faster startup time.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • That's a rather old post (from July 2018) to cite as relevant. GraalVM 19.0.0 (no release candidate mentioned) was released in May 2019. – BoriS Oct 01 '19 at 19:17