3

Java8 provides Next-Generation JavaScript Engine as nashorn. We can get this engine with following code:

ScriptEngineManager engineManager = new ScriptEngineManager(); 
ScriptEngine engine = engineManager.getEngineByName("nashorn");

But i found that javascript and ECMAScript are also valid parameters for getEngineByName()

ScriptEngine engine = engineManager.getEngineByName("javascript");
ScriptEngine engine = engineManager.getEngineByName("ECMAScript");

My queries are:

  • What are the differences between Nashorn, JavaScript and ECMAScript engines?
  • Do java provide other engines?
  • Which one is better to use?
Arun Kumar
  • 323
  • 1
  • 4
  • 15

1 Answers1

3

Javascript and ECMAScript are aliases for default JavaScript engine bundled with JVM. Java 8+ includes Nashorn engine, previous versions were using Rhino engine ("rhino"). Nashorn is much faster than Rhino, because it is compiling JavaScript into bytecode, instead of running in interpreter mode.

The fastest solution I know of to run JavaScript within JVM is J2V8 (https://github.com/eclipsesource/J2V8). JavaScript code runs within the V8 engine, the same one as is used by Node.js and Chrome. In our tests, it's about 2-3 times faster than Nashorn.

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
Piotrek
  • 79
  • 3
  • hmm, I'm not sure about that fast comparison. I just read a [benchmark](https://lemnik.wordpress.com/2017/08/07/is-nashorn-jvm-faster-than-node-v8/) says more than that – Anddo Jul 10 '18 at 21:43