Table of Contents

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.

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:

Nashorn continues to have broad usage, but its use appears to have settled into three main areas:

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