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

New features of Java8 how to use jjs tools

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the new Java8 feature jjs tool". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the new Java8 feature jjs tool".

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:

?

one

two

Function f () {return 1;}

Print (f () + 1)

Run this file and pass it as an argument to jjs

?

one

Jjs func.js

Output: 2

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

?

one

two

three

four

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

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:

?

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

thirty-two

thirty-three

thirty-four

thirty-five

thirty-six

thirty-seven

thirty-eight

thirty-nine

forty

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 ())

}

As can be seen from the code, the test program will execute Esprima's parse and tokenize to run the contents of the test file. Rhino and Nashorn will be executed 30 times respectively. At the beginning, Rhino needs 1726 ms and slowly accelerates, and finally stabilizes at around 950ms. Nashorn has another feature: it takes 3682ms to run * times, but it accelerates quickly after warm-up, and finally stabilizes at 175ms each time, as shown in the following figure.

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

Thank you for your reading, the above is "how to use the new features of Java8 jjs tools" content, after the study of this article, I believe you on the new features of Java8 jjs tools how to use this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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