Server-Side JavaScript with Nashorn

19
Server-Side JavaScript with Nashorn Dan Woods @danveloper

description

Slides from my talk, "Server-Side JavaScript with Nashorn" from MidwestJS

Transcript of Server-Side JavaScript with Nashorn

Server-Side JavaScript with Nashorn

Dan Woods @danveloper

About Me

Author for InfoQ /danveloper

[email protected]

Intro

My Experiences with PhotoZero!!• A client-side file encryption service !• Allowed clients to security unlock files/photos with

confidence !• Server never knew about the content or password

that a user was uploading, only its chunks in the position

!• Salted unlock keys were encrypted to double-down

on security

File

Intro

General PhotoZero Overview

Chunks PZERO Chunking

Engine

Chunk

Chunk

Chunk

Chunk

Server

Encryptor

Encryptor

Encryptor

Encryptor

DB

Backgrounding: https://github.com/danveloper/jquery-bqep-plugin

Intro

PhotoZero & How I came to Server-Side JavaScript!!• Client entirely in HTML5 JavaScript !• Decorated with JQuery (some good, mostly bad) !• Server-side was implemented on the JVM (Grails) !• Non-common language between front-end and

back-end didn’t feel right

Intro

Rhino!!• Started by Netscape in 1997 !• Originally designed as Netscape’s (ahem…)

Javagator !• Code generation, memory leaks, slow compilation

times

Intro

What is Nashorn?!!• The Better-than-Rhino JVM JavaScript Engine !• Comes out-of-the-box with Java 8 and above !• Replacement for Rhino

Intro

The Need for Nashorn!!• JVM is a polyglot platform !• Rhino is old !• Comparatively, Rhino is quite slow !• ECMAScript compliance

Intro

The How of Nashorn!!• Truly practical JavaScript on the JVM was not

possible until Java 7 !• Java 7’s invokeDynamic gives static linking to

dynamic languages (incl. JavaScript) !• JVM dynamic languages have runtime support in

method dispatching, without runtime costs

Engine Features

Nashorn Features!!• 100% ECMAScript 5.1 compliance !• Support for a few extensions

• function expression closures - Mozilla 1.8, • for-each expressions - Mozilla 1.6, • Rhino constructor extensions

!• Powerful CLI (jjs) for building command-line scripts!

Use Cases

Avatar.js and node.jar!!• Avatar is a web framework that brings Node-style

syntax and programming to the JVM !• Written by Oracle as a code-gen’d bridge to HTML5

apps !• Event-driven support for html web server, SSE

spec, websockets

Use Cases

Avatar.js and node.jar!!• Respects your Node modules paths, so you can

npm install in the same way !• Note: v8 and Nashorn have different ideas about

what the Error class does… I’ve built a “fix”

Use Cases

Java Interoperability!!• Java has a great modularity story, and its ability to

compose libraries is a huge value to JavaScript !• Java has a long history and has figured out

solutions to a lot of the problems of server-side development

!• Nashorn’s dynamic linking brings performance with

interoperability; JavaScript methods can be seamlessly linked and vice-versa

Use Cases

What has Java Done Right?!!• For better or worse: databases and persistence !• Threads; we can’t just ignore these things! They’re

useful! !• Right Tool For The Job (tm) — JVM languages !• Embedded systems !• Build systems (Maven, Gradle, SBT, Ant, etc…)

Use Cases

Why Should I Care?!!• This platform of interop allows you to use best-of-

breed for the job !• Gives you an avenue to refactor existing code to

more sensible JavaScript !• Garner all the tooling and monitoring of JMX and

Java instrumentation

Use Cases

Server Side Scripting!!• Nashorn ships with a script runner (jjs) !• Allows server scripts to be written with JavaScript !• Special syntax for falling back to shell, and ability to

capture that output (very Perl-like)

Use Cases

Performance!!• Java 8 introduced practical JavaScript on the JVM !• Performance & Compliance is at the forefront, but

the former is still not quite there !• The near-term roadmap for the JVM brings

performance enhancements to Nashorn

Performance

Performance!!• Soon, JavaScript on the JVM will run at near-native

performance levels! !• This update will come as part of Java 8u40

The Future

The Future…!!• Nashorn will make the JVM a platform for

performant dynamic languages, targeting native-like performance

!• Java 8 will continue to improve upon JavaScript’s

server-side performance, and will offer other languages the same performance