当前位置: 移动技术网 > IT编程>开发语言>Java > Java新特性之Nashorn_动力节点Java学院整理

Java新特性之Nashorn_动力节点Java学院整理

2019年07月22日  | 移动技术网IT编程  | 我要评论

nashorn是什么

nashorn,发音“nass-horn”,是德国二战时一个坦克的命名,同时也是java8新一代的javascript引擎--替代老旧,缓慢的rhino,符合 ecmascript-262 5.1 版语言规范。你可能想javascript是运行在web浏览器,提供对html各种dom操作,但是nashorn不支持浏览器dom的对象。这个需要注意的一个点。

关于nashorn的入门

主要是两个方面,jjs工具以及javax.script包下面的api:

jjs是在java_home/bin下面自带的,作为例子,让我们创建一个func.js, 内容如下:

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

运行这个文件,把这个文件作为参数传给jjs

jjs func.js

输出结果:2

另一个方面是javax.script,也是以前rhino余留下来的api

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;" ) );

输出如下:

jdk.nashorn.api.scripting.nashornscriptengine
result: 2
nashorn vs rhino

javascript运行在jvm已经不是新鲜事了,rhino早在jdk6的时候已经存在,但现在为何要替代rhino,官方的解释是rhino相比其他javascript引擎(比如google的v8)实在太慢了,要改造rhino还不如重写。既然性能是nashorn的一个亮点,下面就测试下性能对比,为了对比两者之间的性能,需要用到esprima,一个ecmascript解析框架,用它来解析未压缩版的jquery(大约268kb),测试核心代码如下:

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首先编译javascript代码为java字节码,然后运行在jvm上,底层也是使用invokedynamic命令来执行,所以运行速度很给力。

为何要用java实现javascript

这也是大部分同学关注的点,我认同的观点是:

1.成熟的gc

2.成熟的jit编译器

3.多线程支持

4.丰富的标准库和第三方库

总得来说,充分利用了java平台的已有资源。

总结

新犀牛可以说是犀牛式战车,比rhino速度快了许多,作为高性能的javascript运行环境,nashorn有很多可能。

举例, avatar.js 是依赖于nashorn用以支持在jvm上实现node.js编程模型,另外还增加了其他新的功能,如使用一个内建的负载平衡器实现多事件循环,以及使用多线程实现轻量消息传递机制;avatar还提供了一个model-store, 基于jpa的纯粹的javascript orm框架。

在企业中另外一种借力 nashorn方式是脚本,相比通常我们使用linux等shell脚本,现在我们也可以使用javascript脚本和java交互了,甚至使用nashorn通过rest接口来监视服务器运行状况。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网