the hidden weapon of JDK 8
lightweight, high perf JavaScript runtime
successor of Rhino (4x faster, ES5.1)
seamless Java <-> JavaScript integration
JS > bytecode > JVM
performance comparison
Rhino 800ms
Nashorn 200ms
V8 100ms
// instantiate Nashorn engine
NashornScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
// load and evaluate JavaScript code
try {
engine.eval(read("path/to/some/javascript/code.js"));
} catch (ScriptException e) {
throw new RuntimeException(e);
}
// execute a function from the loaded JavaScript file
Object result = engine.invokeFunction("someFunction", someDataObject);
// do something with the retrieved result
doSomethingWith(result);
prove that
Nashorn + React.js
is a viable approach
?
✔ ran some JavaScript code through Nashorn
✔ rendered a React component using Nashorn
✔ injected results into Seller login page
(via FreeMarker template)
demo!
create, test a React component (browser)
load, compile in Nashorn (server)
feed it with the same data as FTLs
capture output, inject into page
serve page content
let React continue rendering (browser)
universal JavaScript
server-side-first rendering
SEO (crawlers) and User friendly
fast, cacheable JVM bytecode
FE devs can take complete ownership
better separation between BE / FE
new features via the new approach
running unit test in all browsers + Nashorn
gradual migration of templates to React
full page rendering using Nashorn + React
switch to Node.js later