JavaOne 2014: Java vs JavaScript

100
© 2014 IBM Corporation Chris Bailey – IBM Runtime Monitoring and Diagnostics 1 st October 2014 Java vs JavaScript Head to Head Document number

description

Programmers are often advised to use “the right tool for the right job.” So how does Java compare to JavaScript? This session compares and contrasts Java and JavaScript in different areas and determines just which is the king of the languages that start with Java.

Transcript of JavaOne 2014: Java vs JavaScript

Page 1: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation

Chris Bailey – IBM Runtime Monitoring and Diagnostics

1st October 2014

Java vs JavaScriptHead to Head

Document number

Page 2: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation

Important Disclaimers

THE INFORMATION CONTAINED IN THIS PRESENTATION IS PROVIDED FOR INFORMATIONAL PURPOSES ONLY.

WHILST EFFORTS WERE MADE TO VERIFY THE COMPLETENESS AND ACCURACY OF THE INFORMATION CONTAINED IN THIS PRESENTATION, IT IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.

ALL PERFORMANCE DATA INCLUDED IN THIS PRESENTATION HAVE BEEN GATHERED IN A CONTROLLED ENVIRONMENT. YOUR OWN TEST RESULTS MAY VARY BASED ON HARDWARE, SOFTWARE OR INFRASTRUCTURE DIFFERENCES.

ALL DATA INCLUDED IN THIS PRESENTATION ARE MEANT TO BE USED ONLY AS A GUIDE.

IN ADDITION, THE INFORMATION CONTAINED IN THIS PRESENTATION IS BASED ON IBM’S CURRENT PRODUCT PLANS AND STRATEGY, WHICH ARE SUBJECT TO CHANGE BY IBM, WITHOUT NOTICE.

IBM AND ITS AFFILIATED COMPANIES SHALL NOT BE RESPONSIBLE FOR ANY DAMAGES ARISING OUT OF THE USE OF, OR OTHERWISE RELATED TO, THIS PRESENTATION OR ANY OTHER DOCUMENTATION.

NOTHING CONTAINED IN THIS PRESENTATION IS INTENDED TO, OR SHALL HAVE THE EFFECT OF:

- CREATING ANY WARRANT OR REPRESENTATION FROM IBM, ITS AFFILIATED COMPANIES OR ITS OR THEIR SUPPLIERS AND/OR LICENSORS

2

Page 3: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation3

A Quick Survey

Page 4: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation4

What languages do you program in?

Java JavaScript Both0

10

20

30

40

50

60

Pe

rce

nta

ge

of A

ud

ien

ce

Page 5: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation5

What runtimes do you use?

Server Java Applets JS in Browser Node.js Rhino Nashorn Avatar.js0

10

20

30

40

50

60

70

80

90

100

Pe

rce

nta

ge

of A

ud

ien

ce

Page 6: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation6

Introduction to the speaker Chris Bailey

IBM Runtime Monitoring and Diagnostics Architect- 14 years working with Java and JVM technologies- 6 months working with Node.js and V8

Recent work focus:- Java monitoring, diagnostics and troubleshooting- Java integration into the cloud- JavaScript monitoring, diagnostics and troubleshooting

My contact information:- [email protected] http://www.linkedin.com/in/chrisbaileyibm- http://www.slideshare.net/cnbailey/- @Chris__Bailey

Page 7: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation7

Goals of this talk

● Langage Adoption

● Deployment Modes

● Introduction to Node.js

● Asynchronous IO

● WebApplication Performance

● Under the Hood of Node.js

● JavaScript on the JVM

Page 8: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation8

Language Adoption

Page 9: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation9

GitHub Adoption: Java

Page 10: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation10

GitHub Adoption: JavaScript

GitHut Adoption

Page 11: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation11

Modulecounts.com

Page 12: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation12

StackOverFlow Survey

Page 13: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation13

Tiobe Programming Community Index

● Ratings based on the number of skilled engineers, courses and third party vendors.

Page 14: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation14

Indeed.com Job Trends

Page 15: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation15

Indeed.com Job Trends

Page 16: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation16

Language Adoption JavaScript has a large developer base

- #1 on GitHub with 48.8% more active repositories than Java- #1 on modulecounts.com with 10% more NPM modules than Maven - #1 used language by StackOverflow survey responders- #9 language on the Tiobe index

Java remains a hugely relevant language, particularly on the server - #2 on GitHub with xx% more than the next language- #3 on modulecounts.com with 73.8% more modules than the next language- #2 language on the Tiobe index- #1 on indeed.com for developer jobs

Page 17: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation17

Deployment Modes

Page 18: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation18

Usage in the Browser● JavaScript is ubiquitous in the browser

- Supported in every browser- Full integration with HTML and CSS

● Not affected by negative publicity....

Unless it is absolutely necessary to run Java in web browsers, disable it as describedbelow, even after updating to 7u11. This will help mitigate other Java vulnerabilities thatmay be discovered in the future.

This and previous Java vulnerabilities have been widely targeted by attackers, and new Java vulnerabilities are likely to be discovered. To defend against this and future Java vulnerabilities, consider disabling Java in web browsers…

Page 19: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation19

Usage on the Server● Java has a long history on the server

- JPE launched in 1998

● Java has rich platform support:- Linux x86, Linux POWER, zLinux- Windows, Mac OS, Solaris, AIX, z/OS

JavaScript is a nascent language on the server- Limited platform support – although its growing- No API support to interact with the OS

Part of the browser security model- Frameworks like Node.js have changed that.

Page 20: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation20

Introduction to Node.js Single Threaded Event based JavaScript framework

– Uses non-blocking asynchronous I/O

Wraps the Chrome V8 JavaScript engine with I/O interfaces– Libuv provides interaction with OS/system

● Designed to build scalable network applications– Suited for real time delivery of data to distributed client

Available on a growing set of platforms- Windows, Linux x86, Linux ARM, Mac OS, Solaris- Linux POWER, zLinux, AIX

libuvV8

Node Bindings

Node Standard Library

C

JavaScript

Page 21: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation21

Async I/O Model

Page 22: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation22

Typical approach to IO One thread (or process) per connection

- Each thread waits on a response- Scalability determined by the number of threads

Each thread:- consumes memory- is relatively idle

Number of concurrent customers determined by number of depot workers

Additional customers wait in a queue with no response

Page 23: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation23

Asynchronous non-blocking IO One thread multiplexes for multiple requests

- No waiting for a response- Handles return from I/O when notified

Scalability determined by:- CPU usage- “Back end” responsiveness

Number of concurrent customers determined by how fast the food Server can work

Or until the kitchen gets slammed

Page 24: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation24

Drawbacks of Asynchronous IO Tasks must execute quickly to avoid blocking the event queue

- Analogous to work done under a lock- Stick to the right jobs, eg, I/O- Delegate CPU bound tasks to back end processes

Easy to run out of memory- No direct bound on amount of parallel work- Holding state for each piece or work means unbounded memory usage

Page 25: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation25

JavaScript and Asynchronous IO JavaScript is already event based in the browser

- eg. onClick and onMouseOver events

First class functions and closures fit well with events- Easy to create and pass function callbacks- Easy to execute callbacks in the context of the event

Node.js execution is based on an event loop- Asynchronous I/O built in from the ground up

Node.js execution uses a single thread- No need to worry about locking or shared data- Most machines are now multi-CPU, so cluster capabilities are provided

Page 26: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation26

HTTP Server Examplevar cluster = require('cluster');var cpus = require('os').cpus().length;var http = require('http');

if (cluster.isMaster) { for (var i = 0; i < cpus; i++) { cluster.fork(); } cluster.on('death', function(worker) { console.log("Worker" + worker.pid + "died"); });} else {

http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World!\n"); response.end();

}).listen(8080);}

Page 27: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation27

HTTP Server Example with Clusteringvar cluster = require('cluster');var cpus = require('os').cpus().length;var http = require('http');

if (cluster.isMaster) { for (var i = 0; i < cpus; i++) { cluster.fork(); } cluster.on('death', function(worker) { console.log("Worker" + worker.pid + "died"); });} else {

http.createServer(function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World!\n"); response.end();

}).listen(8080);}

Page 28: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation28

Event Loop Example Hypothetical implementation of readFile():

function readFile(filename, cb) { nb_open(filename, function(err, fd) { if (err) cb(err, null); nb_read(fd, function(err, data) { cb(err, data); nb_close(fd); }); });}

Page 29: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation29

Page 30: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation30

Page 31: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation31

Page 32: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation32

Page 33: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation33

Page 34: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation34

Page 35: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation35

Page 36: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation36

Page 37: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation37

Page 38: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation38

Page 39: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation39

Page 40: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation40

Page 41: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation41

Page 42: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation42

Page 43: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation43

Page 44: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation44

Page 45: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation45

Page 46: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation46

Page 47: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation47

Page 48: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation48

Page 49: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation49

Page 50: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation50

Page 51: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation51

Page 52: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation52

Page 53: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation53

Page 54: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation54

Page 55: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation55

Page 56: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation56

Page 57: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation57

Page 58: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation58

Page 59: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation59

Page 60: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation60

Page 61: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation61

Page 62: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation62

Page 63: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation63

Page 64: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation64

JavaScript and Asynchronous I/O Very little time spent with events on the Event Loop

Provides good scalability, so should provide great performance for IO bound apps

Like WebApplications...

Page 65: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation65

WebApp Performance

Page 66: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation66

JSON Serialization JSON serialization of a newly

instantiated object

Maps- Key of message- Value of Hello, World!

Example response:

Results from TechEmpower.com Round 9 tests (2014-05-01)

Page 67: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation67

JSON Serialization JSON serialization of a newly

instantiated object

Maps- Key of message- Value of Hello, World!

Example response:

Results from TechEmpower.com Round 9 tests (2014-05-01)

Page 68: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation68

Single Query Fetches single row from simple

database table

Row serialized as JSON

Example response:

Results from TechEmpower.com Round 9 tests (2014-05-01)

Page 69: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation69

Single Query Fetches single row from simple

database table

Row serialized as JSON

Example response:

Results from TechEmpower.com Round 9 tests (2014-05-01)

Page 70: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation70

Multiple Query Fetches multiple rows from a

simple database table

Rows serialized as JSON

Example response:

Results from TechEmpower.com Round 9 tests (2014-05-01)

Page 71: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation71

Multiple Query Fetches multiple rows from a

simple database table

Rows serialized as JSON

Example response:

Results from TechEmpower.com Round 9 tests (2014-05-01)

Page 72: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation72

Data Updates Fetches multiple rows from a

simple database table

Converts rows to objects and modifies one attribute of each object

Updates each associated row and serializes as JSON

Example Response:

Results taken from TechEmpower.com

Page 73: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation73

Data Updates Fetches multiple rows from a

simple database table

Converts rows to objects and modifies one attribute of each object

Updates each associated row and serializes as JSON

Example Response:

Results taken from TechEmpower.com

Page 74: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation74

Under the Hood

Page 75: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation75

Polymorphism and Object Representation

Class pointer

Locks

Flags

int

Class pointer

Locks

Flags

int

Class pointer

Locks

Flags

int

Class pointer

Locks

Flags

int

Integer Object Name: Integer

Method Table

Super Class

Field Table

Constant Pool

Object Offsets

...

Integer Class

Java objects are fixed in size and shape- Determined by the object type- Type determined by class pointer

Class pointer gives access to Method Table- All methods executable from the object

Class pointer also gives access to Object Offsets- Fields in the object that are objects- Used for graph traversal during GC

Page 76: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation76

Polymorphism and Object Representation

Field: offset

Field: offset

Field: offset

Map

1:

Length

2:

JSObject

JavaScript objects polymorphic and can change in size and shape

“Simple” objects:- Created with 32 fields- Contains a Map describing key : value

parings- Contains “extra properties” for overflow- Contains “elements” for array values- All fields are 64bits

Multiple maps can exist for “equal” objects- Dependent on order in which fields are added

to the object- ie. X then Y, or Y then X

Map

Elements

Extra Props

1:

2:

3:

...

...

32:

Map

1:

Length

2:

Fixed Array

Fixed Array

Map

Page 77: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation77

Polymorphism and JIT Compilation Functions are stored in JavaScript objects as fields- No fixed set of methods for an object

● Objects are not typed, so data much be checked to determine how to handle iteg. the '+' operator:

- number + number → addition- string involved? → concatenation- objects involved? → convert to primitives then addition or concatenation

eg. property load:- Load prototype object- Load getter method- Load callback function

● Therefore not possible to determine what instructions to use just from the source code

Page 78: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation78

Approach to JIT Compilation V8 JavaScript engine has two compilers

Full Compiler- Compiles JavaScript to native code on first execution- No bytecode stage, and no real optimization applied- Implements a Polymorphic Inline Cache (PIC)

Crankshaft- Execution counter based compilation strategy

Also supports on-stack replacement for code with hot loops- Compilation is done in the execution path- Optimized for types present in the PIC

Page 79: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation79

Polymorphism and Garbage Collection

Implements Generational Garbage Collection, similar to the JVM

– New space, Old space, Large Object space, Remembered sets

– Additional JavaScript specific areas: map space, cell space, property cell space

– Implements concurrent mark/sweep capabilities

Root scan based on Global objects and local variables on the stack

– Reference tree built by following object references to further objects

– However... how do we know which variables are Objects rather than data?

• Solution is tagged pointers – use of 01 in the low order bits to denote a pointer

Page 80: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation80

JavaScript on the JVM?

Page 81: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation81

JavaScript on the JVM Rhino was bundled in JDK6

- Based on the Mozilla Rhino engine- Allowed JavaScript to be embedded in Java, and for JavaScript to call out to Java- Provided jrunscript command line utility

Nashorn replaces JavaScript in JDK8- Supports full ECMAScript 5.1 specification- Exploits invokedynamic to provide 2x to 10x better performance than Rhino- Provides jjs command line utility

Page 82: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation82

JavaScript on the JVM

Rhino was bundled in JDK6ScriptEngineManager scriptEngineManager = new ScriptEngineManager();ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

int sendVal = 7;nashorn.put("sendVal", sendVal);

nashorn.eval(" "+"var Thread = Java.type(\"java.lang.Thread\"); "+"var MyThread = Java.extend(Thread, { "+" run: function() { "+" print(\"Run in separate thread\"); "+" } "+"}); "+"var th = new MyThread(); "+ "th.start(); "+ "th.join(); "+"var resultVal = sendVal + 3;");

System.out.println(nashorn.get("resultVal");

Page 83: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation83

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

int sendVal = 7;nashorn.put("sendVal", sendVal);

nashorn.eval(" "+"var Thread = Java.type(\"java.lang.Thread\"); "+"var MyThread = Java.extend(Thread, { "+" run: function() { "+" print(\"Run in separate thread\"); "+" } "+"}); "+"var th = new MyThread(); "+ "th.start(); "+ "th.join(); "+"var resultVal = sendVal + 3;");

System.out.println(nashorn.get("resultVal");

JavaScript on the JVM

Page 84: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation84

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

int sendVal = 7;nashorn.put("sendVal", sendVal);

nashorn.eval(" "+"var Thread = Java.type(\"java.lang.Thread\"); "+"var MyThread = Java.extend(Thread, { "+" run: function() { "+" print(\"Run in separate thread\"); "+" } "+"}); "+"var th = new MyThread(); "+ "th.start(); "+ "th.join(); "+"var resultVal = sendVal + 3;");

System.out.println(nashorn.get("resultVal");

JavaScript on the JVM

Page 85: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation85

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

int sendVal = 7;nashorn.put("sendVal", sendVal);

nashorn.eval(" "+"var Thread = Java.type(\"java.lang.Thread\"); "+"var MyThread = Java.extend(Thread, { "+" run: function() { "+" print(\"Run in separate thread\"); "+" } "+"}); "+"var th = new MyThread(); "+ "th.start(); "+ "th.join(); "+"var resultVal = sendVal + 3;");

System.out.println(nashorn.get("resultVal");

JavaScript on the JVM

Page 86: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation86

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

int sendVal = 7;nashorn.put("sendVal", sendVal);

nashorn.eval(" "+"var Thread = Java.type(\"java.lang.Thread\"); "+"var MyThread = Java.extend(Thread, { "+" run: function() { "+" print(\"Run in separate thread\"); "+" } "+"}); "+"var th = new MyThread(); "+ "th.start(); "+ "th.join(); "+"var resultVal = sendVal + 3;");

System.out.println(nashorn.get("resultVal");

JavaScript on the JVM

Page 87: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation87

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

int sendVal = 7;nashorn.put("sendVal", sendVal);

nashorn.eval(" "+"var Thread = Java.type(\"java.lang.Thread\"); "+"var MyThread = Java.extend(Thread, { "+" run: function() { "+" print(\"Run in separate thread\"); "+" } "+"}); "+"var th = new MyThread(); "+ "th.start(); "+ "th.join(); "+"var resultVal = sendVal + 3;");

System.out.println(nashorn.get("resultVal");

JavaScript on the JVM

Page 88: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation88

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

int sendVal = 7;nashorn.put("sendVal", sendVal);

nashorn.eval(" "+"var Thread = Java.type(\"java.lang.Thread\"); "+"var MyThread = Java.extend(Thread, { "+" run: function() { "+" print(\"Run in separate thread\"); "+" } "+"}); "+"var th = new MyThread(); "+ "th.start(); "+ "th.join(); "+"var resultVal = sendVal + 3;");

System.out.println(nashorn.get("resultVal");

JavaScript on the JVM

Page 89: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation89

Introduction to Avatar.js Support for Node.js on Nashorn

Binary builds available from Maven– Avatar-js.jar– Avatar-js library (64bit)

Number of common NPM modules are supported

NPM required to dependencies

Issues for native NPMs– No native V8 APIs

From https://avatar-js.java.net/

Page 90: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation90

Avatar.js vs Node.js Running Octane r33

– The “SpecJVM98” of the JavaScript world– Actually a JavaScript benchmark rather than a Node.js benchmark– So more comparing Nashorn to V8

Run on a 8 CPU Windows using:– Node.js v0.10.31– HotSpot 8u20

Settings are “out of the box” with no attempt to tune

Page 91: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation91

Avatar.js running Octane Bencmark

Avatar.js

● Duration: 1m 56s

● Peak memory: 2830MB

Page 92: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation92

Node.js running Octane Bencmark

Node.js

● Duration: 24s

● Peak memory: 268MB

Page 93: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation93

Avatar.js vs Node.js Memory Usage

Avatar.js

● Duration: 1m 56s

● Peak memory: 2830MB

Node.js

● Duration: 24s

● Peak memory: 268MB

Node.js is 4.8x faster

Avatar.js is >10x bigger

Page 94: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation94

Avatar.js vs Node.js: Garbage Collection Avatar.js peak is:

– 920MB / 1.85GB

Node.js peak is:– 200MB / 220MB

Additional 700+MB of Object Heap

Additional 1GB of non-Object Heap

Page 95: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation95

Avatar.js vs Node.js: Stack Traces

Interpretation of JavaScript at the Java layer means stacks are large and anonymous

Page 96: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation96

Summary JavaScript has a large amount of interest and is growing

– Fits well for web applications and code sharing between server and browser– Async IO and event loop model makes it easy to write scalable applications

Weak typing and dynamic linkage makes programming easier, but introduces challenges– Errors found during compilation for strongly typed languages are found at runtime– JIT compilation loves certainty, which is removed

Nashorn and Avatar.js introduces JavaScript to the JVM– Adds interoperability– … but suffers from the same challenges

Page 97: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation97

IBM Developer Kits for Javaibm.biz/javasdk

WebShere Liberty Profilewasdev.net

IBM Bluemixibm.com/bluemix

IBM Developer Kits for Node.jsibm.biz/nodesdk

Page 98: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation98

Questions?

Page 99: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation

ibm.biz/javaone2014Visit Booth 5511 to learn about Cloud, DevOps and Mobile solutions

www.ibm.com/developer

Discovernew technical resources.

Developyour coding skills.

Connectwith developers.

Page 100: JavaOne 2014:   Java vs JavaScript

© 2014 IBM Corporation100

Copyright and Trademarks

© IBM Corporation 2013. All Rights Reserved.

IBM, the IBM logo, and ibm.com are trademarks or registered trademarks of International Business Machines Corp., and registered in many jurisdictions worldwide.

Other product and service names might be trademarks of IBM or other companies.

A current list of IBM trademarks is available on the Web – see the IBM “Copyright and trademark information” page at URL: www.ibm.com/legal/copytrade.shtml