dawidklos
dawidklos

Reputation: 922

Nashorn script caching

Is there a way to cache script evaluated in Nashorn to avoid evaluating same script twice?

For example I have some js library that does something. I want to evaluate Library once and reuse it on every file I want to.

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

Upvotes: 1

Views: 1083

Answers (1)

A. Sundararajan
A. Sundararajan

Reputation: 4405

If you "eval" on same script twice, Nashorn engine will run that script twice! But evaluating twice does not imply compiling (to bytecode) twice. Nashorn does cache compiled representation and reuses.

Upvotes: 3

Related Questions