Friday, 15 June 2012

javascript - How make Java 8 Nashorn fast? -



javascript - How make Java 8 Nashorn fast? -

i'm using java 8 nashorn render commonmark html server side. if compile , cache , reuse compiledscript, page takes 5 minutes render. however, if instead utilize eval, , cache , reuse script engine, rendering same page takes 3 seconds.

why compiledscript slow? (sample code follows)

what's approach running javascript code in nashorn, on , on 1 time again possible? , avoiding compiling javascript code more once?

this server side scala code snippet calls nashorn in way takes 5 minutes: (when run 200 times; i'm compiling many comments commonmark html.) (this code based on this blog article.)

if (engine == null) { val script = scala.io.source.fromfile("public/res/remarkable.min.js").mkstring engine = new js.scriptenginemanager(null).getenginebyname("nashorn") compiledscript = engine.asinstanceof[js.compilable].compile(s""" var global = this; $script; remarkable = new remarkable({}); remarkable.render(__source__);"""); } engine.put("__source__", "**bold**") val htmltext = compiledscript.eval()

edit note $script above reevaluated 200 times. did test version evaluated once, apparently wrote bug, because only-once version wasn't faster 5 minutes, although should have been 1 of fastest ones, see halfbit's answer. here's fast version:

... val newcompiledscript = newengine.asinstanceof[js.compilable].compile(s""" var global; var remarkable; if (!remarkable) { global = this; $script; remarkable = new remarkable({}); } remarkable.render(__source__);""") ...

/edit

whereas takes 2.7 seconds: (when run 200 times)

if (engine == null) { engine = new js.scriptenginemanager(null).getenginebyname("nashorn") engine.eval("var global = this;") engine.eval(new jio.filereader("public/res/remarkable.min.js")) engine.eval("remarkable = new remarkable({});") } engine.put("source", "**bold**") val htmltext = engine.eval("remarkable.render(source)")

i have guessed compiledscript version (the topmost snippet) have been faster. anyway, suppose i'll have cache rendered html server side.

(linux mint 17 & java 8 u20)

update:

i noticed using invokefunction @ end instead of eval twice fast, takes 1.7 seconds. fast java 7 version used javascript code compiled rhino java bytecode (as separate , complicated step in build process). perhaps fast can get?

if (engine == null) { engine = new js.scriptenginemanager(null).getenginebyname("nashorn") engine.eval("var global = this;") engine.eval(new jio.filereader("public/res/remarkable.min.js")) engine.eval("remarkable = new remarkable({});") engine.eval( "function rendercommonmark(source) { homecoming remarkable.render(source); }") } val htmltext = engine.asinstanceof[js.invocable].invokefunction( "rendercommonmark", "**bold1**")

the variant of code uses compiledscript seems re-evaluate remarkable.min.js 200 times - while eval based version once. explains huge difference in runtimes.

with remarkable.render(__source__) precompiled, compiledscript based variant faster eval , invokefunction based ones (on machine, oracle java 8u25).

java javascript performance nashorn

No comments:

Post a Comment