Slide 1

Slide 1 text

ISOMORPHIC JAVASCRIPT WITH NASHORN Maxime Najim Monday, October 26, 2015

Slide 2

Slide 2 text


 var me = { name: “Maxime Najim”, title: “Software Architect”, work: “@WalmartLabs”, org: “@Platform” twitter: “@softwarecrafts” } About Me Final Release Date: April 2016

Slide 3

Slide 3 text

Why am I talking 
 about JavaScript 
 at JavaOne?

Slide 4

Slide 4 text

"Java is to JavaScript as ham is to hamster" Jeremy Keith http://javascriptisnotjava.io

Slide 5

Slide 5 text

Three reasons why Java developers should be talking 
 about JavaScript…

Slide 6

Slide 6 text

Reason 1:
 Developers from different backgrounds are converging on JavaScript

Slide 7

Slide 7 text

RedMonK Programming 
 Language Rankings Top 20 1 JavaScript 2 Java 3 PHP 4 Python 5 C# 5 C++ 5 Ruby 8 CSS 9 C 10 Objective-C 11 Perl 11 Shell 13 R 14 Scala 15 Go 15 Haskell 17 Matlab 18 Swift …

Slide 8

Slide 8 text

source: modulecounts.com JavaScript’s standard package repository is the fasting growing and most active package public repository. 
 * More people are actively working on JavaScript projects
 * More likely to find open-source solutions

Slide 9

Slide 9 text

Reason 2: 
 JavaScript is the platform for building rich and highly interactive web apps

Slide 10

Slide 10 text

In the past decade, we’ve seen the 
 Web evolve…

Slide 11

Slide 11 text

The Web is no longer simply documents 
 linked together

Slide 12

Slide 12 text

Web Evolution 1990’s - Initial Web Era The world's first web page: http://info.cern.ch/hypertext/WWW/TheProject.html

Slide 13

Slide 13 text

Web Evolution 2000’s - AJAX Web Era Gmail (2004)

Slide 14

Slide 14 text

Web Evolution 2010’s - Single Page App Web Era

Slide 15

Slide 15 text

JavaScript has 
 enabled Web sites to evolve to web apps

Slide 16

Slide 16 text

JavaScript in the browser has become our app runtime environment

Slide 17

Slide 17 text

Reason 3: 
 JavaScript isn’t only 
 for the browser

Slide 18

Slide 18 text

When people think of JavaScript they 
 think of browser 
 provided APIs 
 (e.g. window, document, etc.)

Slide 19

Slide 19 text

Rendering Engine 
 
 (HTML, CSS) Scripting Engine (JavaScript) Web Browser

Slide 20

Slide 20 text

Rendering Engine 
 
 (HTML, CSS) Scripting Engine (JavaScript) Web Browser Browser APIs (Document, Window, Navigator)

Slide 21

Slide 21 text

Gecko
 
 (HTML, CSS) Spider Monkey (JavaScript) Firefox Trident
 
 (HTML, CSS) Chakra (JavaScript) IE Blink
 
 (HTML, CSS) V8 (JavaScript) Chrome WebKit (HTML, CSS) Nitro (JavaScript) Safari

Slide 22

Slide 22 text

V8 (JavaScript) libuv (non-blocking I/O) Rendering Engine Scripting Engine Web Browser

Slide 23

Slide 23 text

V8 (JavaScript) libuv (non-blocking I/O) Rendering Engine Scripting Engine Web Browser

Slide 24

Slide 24 text

Isomorphic JavaScript a.k.a Universal JavaScript, Portable JavaScript, Shared JavaScript

Slide 25

Slide 25 text

Isomorphic JavaScript Web Mobile IoT Client Server JavaScript code that runs both on the backend web application server and the client.

Slide 26

Slide 26 text

Isomorphic JavaScript 1. Staying DRY (Don’t-Repeat-Yourself) - using the same code base improves code maintenance. 2.Server Side Rendering of Single Page Applications
 (very critical to the business) Advantages

Slide 27

Slide 27 text

1) Staying DRY with Isomorphic JavaScript

Slide 28

Slide 28 text

Staying DRY Client Server Models Models Views Views Routing Controllers Routing Controllers Fetching Fetching Views Logic Views Logic i18n/ l10n i18n/ l10n

Slide 29

Slide 29 text

Staying DRY Client Server Models Models Views Routing Controllers Routing Controllers Fetching Fetching Views Logic Views Logic i18n/ l10n i18n/ l10n Logic-less 
 Templates

Slide 30

Slide 30 text

Staying DRY Client Server Models Models Views Routing Controllers Routing Controllers Fetching Fetching Views Logic i18n/ l10n i18n/ l10n

Slide 31

Slide 31 text

Staying DRY Client Server Models Models Views Routing Controllers Routing Controllers Fetching Fetching Views Logic i18n/ l10n

Slide 32

Slide 32 text

Staying DRY Client/Server Models Views Routing Controllers Fetching Views Logic i18n/ l10n

Slide 33

Slide 33 text

2) Server Side Rendering of Single Page Applications

Slide 34

Slide 34 text

Web Evolution 1990’s - Initial Web Era Client-Server Model

Slide 35

Slide 35 text

Web Evolution 2000’s - AJAX Era Client-Server Model

Slide 36

Slide 36 text

Web Evolution 2010’s - Single Page App Era Client-Server Model

Slide 37

Slide 37 text

Single Page App Initial Server Markup

Slide 38

Slide 38 text

1. Download skeleton HTML 2. Download the 
 JavaScript 3. Evaluate
 JavaScript 4. Fetch Data 
 from the API Single Page App Rendering Flow

Slide 39

Slide 39 text

Timeline Single Page App

Slide 40

Slide 40 text

Increasing User Demand www.radware.com Single Page App

Slide 41

Slide 41 text

Time is money •For every 1 second of improvement, experienced up to a 2% increase in conversions •For every 100 ms of improvement, grew incremental revenue by up to 1% Single Page App Source: http://www.globaldots.com/how-website-speed-affects-conversion-rates

Slide 42

Slide 42 text

Isomorphic Rendering 1. Render the HTML of a JavaScript app on the Server
 2. Return the full HTML on a new page request 3. JavaScript loads and bootstraps the application (without destroying and rebuilding the initial HTML) JavaScript rendered on the server and the client.

Slide 43

Slide 43 text

Isomorphic Rendering 1. Download skeleton HTML 2. Download the 
 JavaScript 3. Evaluate 
 JavaScript

Slide 44

Slide 44 text

Isomorphic JavaScript • Important for initial page load performance 
 • Important for Search Engine Indexing and Optimization (SEO) 
 • Important for mobile users with low bandwidth • Important for code maintenance

Slide 45

Slide 45 text

Isomorphic JavaScript sounds amazing but…

Slide 46

Slide 46 text

What if my
 front-end servers 
 are running on Java (and are battle tested in production)

Slide 47

Slide 47 text

Three possible 
 solutions…

Slide 48

Slide 48 text

Option 1: 
 Delegate execution of JavaScript to an external process running Node.js

Slide 49

Slide 49 text

Delegate to Node.js FE Server render 
 (comp) HTML Fetch Page HTML Downsides: • more complicated deployments • performance overhead of interacting with an external process

Slide 50

Slide 50 text

Option 2:
 Have Node run as a smart-proxy in 
 front of Java

Slide 51

Slide 51 text

Node as a smart-proxy Fetch Page HTML REST JSON Downsides: • more complicated deployments • performance overhead of interacting with an external process

Slide 52

Slide 52 text

Option 3:
 Run JavaScript on the JVM with Nashorn

Slide 53

Slide 53 text

V8 (JavaScript) libuv (non-blocking I/O) Rendering Engine Scripting Engine Web Browser Nashorn (JavaScript)

Slide 54

Slide 54 text

Java 8 Nashorn •Java’s embedded JavaScript engine that comes part of Java 8 (replacing Rhino). •Nashorn supports the full ECMAScript 5.1 specification plus some extensions. (Future versions of Nashorn (Java 9) will include support for ECMAScript 6).
 •It compiles JavaScript to Java bytecode providing interoperability between Java and JavaScript code

Slide 55

Slide 55 text

Java 8 Nashorn • Automatic memory management 
 • State of the art JIT optimizations
 • Man decades of high tech and tooling
 Code Base Comparison

Slide 56

Slide 56 text

Java 8 Nashorn Javascript code can either be evaluated directly by passing javascript strings: Or by passing a file reader pointing to a .js script file:

Slide 57

Slide 57 text

Java 8 Nashorn Invoking JavaScript functions from Java: Java Code Output JS Code

Slide 58

Slide 58 text

Java 8 Nashorn Sharing Internationalization (i18n) code: Java Code Java JS Code JavaScript

Slide 59

Slide 59 text

Java 8 Nashorn Sharing View Logic Code: Java Code JS Code Template Output

Slide 60

Slide 60 text

Java 8 Nashorn Sharing Validation Code: JS Code Java Code Client Validation Server Validation (if Client validation is bypassed)

Slide 61

Slide 61 text

Server-side React • React.renderToString(..) - returns a string of the rendered component Component Output data-react-checksum: checksum of the DOM that is created. This allows React to reuse the DOM from the server when rendering the same component on the client.

Slide 62

Slide 62 text

Java 8 Nashorn Server-Side rendering of React.js components from Java: Script Engine HTML context (similar to node’s vm module runInThisContext(..)) Output Invoke comp1.jsx compN.jsx

Slide 63

Slide 63 text

Java 8 Nashorn Client-Side transition from server-side rendered components Client Code Output Bootstrap

Slide 64

Slide 64 text

Nashorn Concurrency • In web browsers, there is no concurrent execution of your code. • Thread-safety depends on your Javascript code. Nashorn itself will not make your code thread-safe. • Use a ThreadLocal when Javascript code is not thread-safe (i.e. Handlebars and React).

Slide 65

Slide 65 text

Nashorn Performance Execution Time (ms) 0 75 150 225 300 Number of Executions 1 10 100 1000 Nashorn NodeJS https://github.com/maximenajim/java-vs-node-react-rendering-microbenchmark extra warm-up time

Slide 66

Slide 66 text

Nashorn Performance https://www.youtube.com/watch?v=aROpSjXr4TU JVMLS 2015 - Nashorn for Java 9 - Marcus Lagergren

Slide 67

Slide 67 text

Demo • https://github.com/maximenajim/isomorphic-javascript-nashorn-example • https://github.com/maximenajim/isomorphic-flux-javascript-nashorn-example • https://github.com/maximenajim/isomorphic-validation-nashorn-example

Slide 68

Slide 68 text

Nashorn Adoption Spectrum Java JavaScript Duplicated 
 Logic Shared 
 Logic Multi-Page App Single-Page App Server-Side Only App Isomorphic JavaScript App

Slide 69

Slide 69 text

Free O’Reilly Report Free Download: http://www.oreilly.com/web-platform/free/why-isomorphic-javascript.csp The Golden Age of JavaScript began when web developers traded in their fat-server, thin-client approach for desktop-like web apps running in the browser. Unfortunately, that approach led to a succession of problems, so now the pendulum is swinging back in the other direction. Companies such as Walmart, Airbnb, Facebook, and Netflix have already adopted a new solution using JavaScript code on both the client and server. Authors Jason Strimpel and Maxime Najim from WalmartLabs explain that isomorphic JavaScript is the latest in a series of engineering fixes that brings a harmonious equilibrium between the fat-server, fat-client pendulum, which emerged from the Ajax and Single Page Application eras.

Slide 70

Slide 70 text

Thank You @softwarecrafts www.oreilly.com/pub/au/6521 https://github.com/maximenajim