【发布时间】: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