【问题标题】:I want to speed up the eval of Nashorn我想加快 Nashorn 的评估
【发布时间】:2015-11-17 09:49:28
【问题描述】:

我被允许将zxcvbn.js (Javascript library) 运行到 Nashorn。 但是有一个问题。

eval(预编译)非常慢。大约需要 3 分钟。 我想更快地移动。

public class StrengthChecker {

  private static final String ZXCVBN_PATH = "/META-INF/resources/webjars/zxcvbn/1.0/zxcvbn.js";

  private final ScriptEngine engine;

  public StrengthChecker() {
    ScriptEngineManager manager = new ScriptEngineManager();
    engine = manager.getEngineByName("nashorn");

    Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    engineScope.put("window", engineScope);

    try {

      // -------------------------------------------
      // Here is very slow definition of zxcvbn.js.
      // -------------------------------------------
      engine.eval(getResourceContents(ZXCVBN_PATH));

    } catch (ScriptException e) {
      throw new RuntimeException(e);
    }
  }

  public Strength check(String pw) {
    try {
      Map<String, Object> result;
      result = (Map<String, Object>) engine.eval("zxcvbn('" + pw + "');");

      return new Strength(
        ((Double) result.get("entropy")).intValue(),
        (int) result.get("score"),
        ((Double) result.get("crack_time")).intValue()
      );
    } catch (ScriptException e) {
      throw new RuntimeException(e);
    }
  }

}

请告诉我们一些解决方案。

【问题讨论】:

  • 那么当你在浏览器而不是 Nashorn 中运行 javascript 代码时,它不会那么慢吗?
  • 浏览器速度快的时候。而在Nashorn中运行时速度更快。但是在 Nashorn 中加载库(zxcvbn.js)时会很慢。

标签: javascript java nashorn


【解决方案1】:

这是一个已修复的已知性能错误,请参阅https://bugs.openjdk.java.net/browse/JDK-8137333。它应该与 Java 8u72 一起发布,计划于 2016 年 1 月发布。https://jdk9.java.net/download/ 上提供的 Java 9 的预发布版本也包含修复(自 JDK9 版本 b85 起)。

【讨论】:

  • 谢谢你给我很好的答案。感谢您的好意。
猜你喜欢
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-02
  • 1970-01-01
相关资源
最近更新 更多