我想加快纳霍恩的评估

I want to speed up the eval of Nashorn

本文关键字:评估      更新时间:2023-09-26

我被允许运行zxcvbn.js(Javascript库)到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);
    }
  }
}

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

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