Nashorn 脚本缓存
Nashorn script caching
有没有一种方法可以缓存在 Nashorn
中评估的脚本以避免两次评估相同的脚本?
例如,我有一些 js 库可以做一些事情。我想对 Library 进行一次评估,然后在我想要的每个文件上重复使用它。
public void modifyFile(String inputFile){
scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
bindings = scriptEngine.createBindings();
scriptEngine.eval(new FileReader("css-lib.js"),bindings);
bindings.put(INPUT, readFile(inputFilePath, StandardCharsets.UTF_8));
scriptEngine.getContext().setWriter(new StringWriter());
executeScript(COMPRESS_SCRIPT_PATH, scriptEngine, bindings);
}
如果您对同一个脚本 "eval" 两次,Nashorn 引擎将 运行 该脚本两次!但是评估两次并不意味着编译(到字节码)两次。 Nashorn 确实缓存已编译的表示和重用。
有没有一种方法可以缓存在 Nashorn
中评估的脚本以避免两次评估相同的脚本?
例如,我有一些 js 库可以做一些事情。我想对 Library 进行一次评估,然后在我想要的每个文件上重复使用它。
public void modifyFile(String inputFile){
scriptEngine = new ScriptEngineManager().getEngineByName("nashorn");
bindings = scriptEngine.createBindings();
scriptEngine.eval(new FileReader("css-lib.js"),bindings);
bindings.put(INPUT, readFile(inputFilePath, StandardCharsets.UTF_8));
scriptEngine.getContext().setWriter(new StringWriter());
executeScript(COMPRESS_SCRIPT_PATH, scriptEngine, bindings);
}
如果您对同一个脚本 "eval" 两次,Nashorn 引擎将 运行 该脚本两次!但是评估两次并不意味着编译(到字节码)两次。 Nashorn 确实缓存已编译的表示和重用。