Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the new Java 8 feature, Nashorn?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the new Java 8 feature Nashorn is like, which is concise and easy to understand. I hope you can get something through the detailed introduction of this article.

What is Nashorn?

Nashorn, pronounced "nass-horn", is the name of a German tank during World War II. It is also a new generation of javascript engine for java8-replacing the old, slow Rhino and conforming to the ECMAScript-262 version 5.1 language specification. You may want javascript to run in a web browser that provides various dom operations on html, but Nashorn does not support browser DOM objects. This is a point to pay attention to.

About the introduction to Nashorn

There are mainly two aspects, the jjs tool and the API under the javax.script package:

Jjs is shipped under java_home/bin. As an example, let's create a func.js with the following content:

Function f () {return 1;}; print (f () + 1)

Run this file and pass it as an argument to jjs

Jjs func.js

Output: 2

Another aspect is javax.script, which is also the API left over from the previous Rhino.

ScriptEngineManager manager = new ScriptEngineManager (); ScriptEngine engine = manager.getEngineByName ("JavaScript"); System.out.println (engine.getClass (). GetName ()); System.out.println ("Result:" + engine.eval ("function f () {return 1;}; f () + 1;"))

The output is as follows:

Jdk.nashorn.api.scripting.NashornScriptEngine

Result: 2

You can also refer to this blog post on http://my.oschina.net/jsmagic/blog/212455 for basic usage.

Nashorn VS Rhino

It is nothing new for javascript to run on jvm. Rhino has existed since jdk6, but now the official explanation is that Rhino is so slow compared to other javascript engines (such as google's V8) that it is better to rewrite Rhino than other javascript engines. Since performance is a highlight of Nashorn, let's test the performance comparison. In order to compare the performance between the two, you need to use Esprima, an ECMAScript parsing framework, to parse the uncompressed version of jquery (about 268kb). The core test code is as follows:

Static void rhino (String parser, String code) {String source = "speedtest"; int line = 1; Context context = Context.enter (); context.setOptimizationLevel (9); try {Scriptable scope = context.initStandardObjects (); context.evaluateString (scope, parser, source, line, null) ScriptableObject.putProperty (scope, "$code", Context.javaToJS (code, scope)); Object tree = new Object (); Object tokens = new Object (); for (int I = 0; I

< RUNS; ++i) { long start = System.nanoTime(); tree = context.evaluateString(scope, "esprima.parse($code)", source, line, null); tokens = context.evaluateString(scope, "esprima.tokenize($code)", source, line, null); long stop = System.nanoTime(); System.out.println("Run #" + (i + 1) + ": " + Math.round((stop - start) / 1e6) + " ms"); } } finally { Context.exit(); System.gc(); } } static void nashorn(String parser, String code) throws ScriptException,NoSuchMethodException { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("nashorn"); engine.eval(parser); Invocable inv = (Invocable) engine; Object esprima = engine.get("esprima"); Object tree = new Object(); Object tokens = new Object(); for (int i = 0; i < RUNS; ++i) { long start = System.nanoTime(); tree = inv.invokeMethod(esprima, "parse", code); tokens = inv.invokeMethod(esprima, "tokenize", code); long stop = System.nanoTime(); System.out.println("Run #" + (i + 1) + ": " + Math.round((stop - start) / 1e6) + " ms"); } // System.out.println("Data is " + tokens.toString() + " and " + tree.toString()); } 从代码可以看出,测试程序将执行Esprima的parse和tokenize来运行测试文件的内容,Rhino和Nashorn分别执行30次,在开始时候,Rhino需要1726 ms并且慢慢加速,最终稳定在950ms左右,Nashorn却有另一个特色,***次运行耗时3682ms,但热身后很快加速,最终每次运行稳定在175ms,如下图所示

Nashorn first compiles the javascript code to java bytecode, and then runs it on jvm, which is also executed using the invokedynamic command at the bottom, so it runs very fast.

Why use java to implement javascript

This is also what most students are concerned about. I agree with this point of view:

Mature GC

Mature JIT compiler

Multithreading support

Rich standard libraries and third-party libraries

In general, it makes full use of the existing resources of the java platform.

The new rhino can be said to be a rhino chariot, much faster than Rhino, as a high-performance javascript operating environment, Nashorn has many possibilities.

For example, Avatar.js relies on Nashorn to support the implementation of the Node.js programming model on JVM, and other new features are added, such as using a built-in load balancer to achieve multi-event loops, and the use of multi-threading to achieve lightweight messaging mechanism; Avatar also provides a Model-Store, a pure JPA-based JavaScript ORM framework.

Another way to borrow Nashorn in an enterprise is scripting. Instead of using shell scripts such as Linux, we can now use Javascript scripts to interact with Java, and even use Nashorn to monitor server health through the REST interface.

The above is what the new Java 8 feature Nashorn is like. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report