Javascript - Nashorn Engine
About
Nashorn is the JDK’s built-in JavaScript engine. Rhino was its predecessor.
Background: The development of Nashorn started in late 2010 to experiment with the invokedynamic (JSR 292) byte-code instruction.
Because JavaScript would likely grow to dominate client-side development, integrating it with Java would be a critical element in a client/server equation.
In November 2012, JDK Enhancement Proposal (JEP) 174, “Nashorn JavaScript Engine,” was approved for a full-featured implementation of ECMAScript-262 Edition 5.1 (ES5) to run on the Java platform.
Articles Related
Example
Inside java
ScriptEngine engine = new ScriptEngineManager().
getEngineByName("nashorn");
engine.eval("print('Hello World');");
Jjs Shell
See Javascript - jjs
Application
Initially, Nashorn found a home in a wide variety of applications, such as:
- app servers,
- JavaFX applications,
- utilities,
- shell scripts,
- embedded systems,
- and so on.
Nashorn continues to have broad usage, but its use appears to have settled into three main areas:
- The development of JavaScript applications that can be run on both the client and the server. In the JavaScript world, this is known as isomorphic development. The advantages are huge for smaller shops that want to build front ends for both desktop and mobile services—a single programming language with a common codebase and rapid delivery. Isomorphic development also scales well for larger systems.
- Runtime adaptive or dynamic coding. The term I like to use is soft coding, where portions of an application can be modiied after the application/server is deployed. This capability is used for everything from stored procedures in databases to application coniguration management.
- Shell scripting. This consists of using JavaScript in areas where bash or Python would traditionally be used.
Nashorn vs V8
Nashorn’s performance on the JVM compared to native JavaScript performance on platforms such as Google’s V8.
Nashorn starts out slower because it translates the JavaScript to bytecode. After that, Nashorn is at the mercy of HotSpot to deliver native code.
This approach works well for long-running server applications but not as well for small and run-once scripts. The main reason Nashorn was developed in the first place was to run JavaScript and Java together seamlessly. Most Nashorn applications do a great job of orchestrating JavaScript and Java, something that V8 was not designed to do.
Used by
Documentation / Reference
- nashorn: Java Embedded Script Engine in Java 8