java vs. java script for enterprise web applications - chris bailey
TRANSCRIPT
© 2015 IBM Corporation1
Java vs JavaScriptFor Enterprise Web Applications
Chris BaileyIBM Runtime Technologies
© 2015 IBM Corporation2
What Languages do you use?
Java JavaScript Both0
20
40
60
80
100
120
Pe
rce
nta
ge
of A
ud
ien
ce
© 2015 IBM Corporation3
What Runtimes do you use?
Server Java Applets JS in Browser Node.js Rhino Nashorn Avatar.js0
20
40
60
80
100
120
Pe
rce
nta
ge
of A
ud
ien
ce
© 2015 IBM Corporation4 4
Chris BaileyIBM Runtime Monitoring and Diagnostics Architect
–14 years working with Java and JVM technologies–14 months working with Node.js and V8
Current Role(s):–IBM Java monitoring and diagnostics
–Node Application Metrics project lead
Node Foundation MemberBenchmarking and Performance WGs
JavaOne RockStarAuthor on performance and memory analysis
[email protected]@seabaylea@Chris__Bailey
© 2015 IBM Corporation5
Language Adoption
Deployment Modes
Code Development
Scalability
WebApplication Performance
Under the Hood
Enterprise Architectures
Agenda
© 2015 IBM Corporation11
Ratings based on the number of skilled engineers, courses and third party vendors.
Tiobe Community Programming Index
© 2015 IBM Corporation15
JavaScript has a large developer base- #1 on GitHub with 45% more active repositories than Java- #1 on modulecounts.com with 29% more NPM modules than
Maven - #1 used language by StackOverflow survey responders- #6 language on the Tiobe index
Java remains huge, particularly in the enterprise and on the server - #2 on GitHub with 52% more active repositories than the next
language- #3 on modulecounts with 73.8% more modules than the next
language- #2 language on the Tiobe index- #1 on indeed.com for developer jobs
Language Adoption
© 2015 IBM Corporation17
JavaScript is ubiquitous in the browser- Supported in every browser
- Integration with HTML and CSS
JavaScript is 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…
Usage in the browser
© 2015 IBM Corporation18
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.
Usage on the server
© 2015 IBM Corporation19
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 wide set of platforms:- Linux on x86, ARM, Power and Z- Windows, Mac OS, Solaris, SmartOS and AIX
libuvV8
Node Bindings
Node Standard Library
C
JavaScript
Server Side JavaScript: Node.js
© 2015 IBM Corporation20
Anatomy of a Node.js application1. package.json
– Tracks properties, dependencies, version info, commands, etc.
2. JavaScript source files (i.e. app.js)
•To install and execute:
> npm install
> node app.js
20
© 2015 IBM Corporation22
var 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);}
HTTP Server Example
© 2015 IBM Corporation23
var 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);}
Clustered HTTP Server Example
© 2015 IBM Corporation24
Writing Node.js Code
Co
de
Vo
lum
e R
ela
tive
to
Ja
va
* based on benchmarksgame benchmarks
Node.js
Development Effort(lower is better)
- 3x
*or other transactional service
© 2015 IBM Corporation25
Writing Node.js Code
regex-dna Spectral-norm fannkuch-redux fasta k-nucleotide Binary-trees n-body Reverse-complement
Co
de
Vo
lum
e R
ela
tive
to
Ja
va
* based on benchmarksgame benchmarks
Node.js
Development Effort(lower is better)
- 3x
Avg 1/3rd less code
*or other transactional service
© 2015 IBM Corporation26
Writing Node.js Code
regex-dna Spectral-norm fannkuch-redux fasta k-nucleotide Binary-trees n-body Reverse-complement
Co
de
Vo
lum
e R
ela
tive
to
Ja
va
* based on benchmarksgame benchmarks
Node.js
Development Effort(lower is better)
- 3x
● Node.js has higher developer productivity Many applications developed with significantly less code
● Rich module system simplifies development Reduces need to develop custom code
Avg 1/3rd less code
*or other transactional service
© 2015 IBM Corporation28
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
Typical approach to I/O
© 2015 IBM Corporation29
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
Asycnhronous Non-Blocking I/O
© 2015 IBM Corporation30
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
JavaScript and Asynchronous I/O
© 2015 IBM Corporation31
var http = require('http');
var server = http.createServer();server.listen(8080);
server.on('request', function(request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("Hello World!\n"); response.end();});
server.on('connection', function(socket) {});server.on('close', function() {});server.on('connect', function(socket) {});server.on('upgrade', function(request, socket, head) {});server.on('clientError', function(exception, socket) {});
Event based programming
© 2015 IBM Corporation33
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)
JSON Serialization
© 2015 IBM Corporation34
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)
JSON Serialization
JavaScript
Java
© 2015 IBM Corporation35
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
40
-75
Node.js Performance
(Compared to Java)
JSON Serialization
%a
ge
of J
ava
Pe
rfo
rma
nce
© 2015 IBM Corporation36
Fetches single row from simple database table
Row serialized as JSON
Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
Single Query
© 2015 IBM Corporation37
Fetches single row from simple database table
Row serialized as JSON
Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
Single Query
JavaScript
Java
© 2015 IBM Corporation38
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
40
-60.5
Node.js Performance
(Compared to Java)
JSON Serialization
Single Query
%a
ge
of J
ava
Pe
rfo
rma
nce
© 2015 IBM Corporation39
Fetches multiple rows from a simple database table
Rows serialized as JSON
Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
Multiple Queries
© 2015 IBM Corporation40
Fetches multiple rows from a simple database table
Rows serialized as JSON
Example response:
Results from TechEmpower.com Round 9 tests (2014-05-01)
Multiple Queries
JavaScript
Java
© 2015 IBM Corporation41
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
40
-18
Node.js Performance
(Compared to Java)
JSON Serialization
Single Query
Multiple Queries
%a
ge
of J
ava
Pe
rfo
rma
nce
© 2015 IBM Corporation42
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:
Data Updates
Results from TechEmpower.com Round 9 tests (2014-05-01)
© 2015 IBM Corporation43
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:
Data Updates
Results from TechEmpower.com Round 9 tests (2014-05-01)
JavaScript
Java
© 2015 IBM Corporation44
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
4028
Node.js Performance
(Compared to Java)
JSON Serialization
Single Query
Multiple Queries
Data Updates
%a
ge
of J
ava
Pe
rfo
rma
nce
© 2015 IBM Corporation45
Computation speed is (much) slower than Java
I/O speed is higher than Java
JavaScript WebApp Performance
-100
-80
-60
-40
-20
0
20
40
-75 -60.5 -18
28
Node.js Performance
(Compared to Java)
JSON Serialization
Single Query
Multiple Queries
Data Updates
%a
ge
of J
ava
Pe
rfo
rma
nce
MoreComputation
MoreI/O
© 2015 IBM Corporation47
Nashorn and Avatar.js Nashorn JavaScript engine delivered in JDK8
– Utilizes new JVM level features for performance
Avatar.js provides Node.js support on Nashorn
© 2015 IBM Corporation48
Nashorn and Avatar.js Nashorn JavaScript engine delivered in JDK8
– Utilizes new JVM level features for performance
Avatar.js provides Node.js support on Nashorn
Results of “Octane” JavaScript benchmark*:
– Node.js is 4.8x faster
– Avatar.js is >10x larger
* Using Java 8 pre-u20
© 2015 IBM Corporation49
Nashorn and Avatar.js Nashorn JavaScript engine delivered in JDK8
– Utilizes new JVM level features for performance
Avatar.js provides Node.js support on Nashorn
Results of “Octane” JavaScript benchmark*:
– Node.js is 4.8x faster
– Avatar.js is >10x larger
* Using Java 8 pre-u40
Feb 12th, 2015: Avatar is “put on hold”https://blogs.oracle.com/theaquarium/entry/project_avatar_update
© 2015 IBM Corporation51
Dynamic nature of JavaScript makes JIT optimizations more difficult
– Any variable could be a function or data
– Variables must be tested to determine how to handle handle them
– Variables can change, so what worked previously many not apply in future
Example: the use of '+':
• number + number → addition
• string involved? → concatenation
• objects involved? → convert to primitives then addition or concatenation
• Functions involved? → ?
JIT Compilation
© 2015 IBM Corporation52
Execution flow for '+' operator
int aint bint c = a + b
Addition
Return Result
Java
© 2015 IBM Corporation53
Execution flow for '+' operator
int aint bint c = a + b
Addition
Return Result
var avar bvar c = a + b
First Time?
NO
Use Previous Types
Worked?
Return Result
YES
Type of A?
NO
YES
Type of B?String ConcatenateString
orNumber
Number Type of B? Number Addition
UndefinedString
Java JavaScript
© 2015 IBM Corporation54
Dynamically typed languages are harder to manage under the hood
They subsequently have lower runtime performance for computational tasks
Highlighted in TechEmpower benchmarks:
Dynamically vs Statically Types Languages
-70
-60
-50
-40
-30
-20
-10
0
Dynamic vs Statically Typed Language Performance
JSON
Single
Multi
Updates
Be
st d
yna
mic
co
mp
are
d to
be
st s
tatic
as
ba
se
line
© 2015 IBM Corporation56
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation57
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation58
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation59
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation60
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation61
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation62
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation63
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation64
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation65
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation66
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation67
Not So Easy: JavaScript Syntax Fun
> '5' – 32 // Weak typing, implicit conversion
> '5' + 3'53' // ...but not consistent
> '5' – '4'1 // String minus String = Integer??
> '5' + + '4'54 // Multiple +'s are ok
> 'Hello' + 'World''HelloWorld' // Ok, that's expected
> 'Hello' + + 'World''HelloNaN' // ...but that isn't
© 2015 IBM Corporation68
Not So Easy: JavaScript Syntax Fun
> '5' + - '5''5-2' // I can just about see that works
> var x = 3undefined> '5' – x + x5 // Ok, that makes sense
> var x = 3undefined> '5' + x - x50 // What???
© 2015 IBM Corporation69
Not So Easy: JavaScript Syntax Fun
> '5' + - '5''5-5' // I can just about see that works
> var x = 3undefined> '5' – x + x5 // Ok, that makes sense
> var x = 3undefined> '5' + x - x50 // What???
© 2015 IBM Corporation70
Not So Easy: JavaScript Syntax Fun
> '5' + - '5''5-2' // I can just about see that works
> var x = 3undefined> '5' – x + x5 // Ok, that makes sense
> var x = 3undefined> '5' + x - x50 // What???
© 2015 IBM Corporation71
Not So Easy: JavaScript Syntax Fun
> '5' + - '5''5-2' // I can just about see that works
> var x = 3undefined> '5' – x + x5 // Ok, that makes sense
> var x = 3undefined> '5' + x - x50 // What???
© 2015 IBM Corporation72
Not So Easy: JavaScript Syntax Fun
> '5' + - '5''5-2' // I can just about see that works
> var x = 3undefined> '5' – x + x5 // Ok, that makes sense
> var x = 3undefined> '5' + x - x50 // What???
© 2015 IBM Corporation73
Not So Easy: JavaScript Syntax Fun
> '5' + - '5''5-2' // I can just about see that works
> var x = 3undefined> '5' – x + x5 // Ok, that makes sense
> var x = 3undefined> '5' + x - x50 // What???
© 2015 IBM Corporation75
Enterprise Application Platform – Web Applications
Operations and Management
Admin Analytics
Lo
ad B
alan
cer
Lo
ad B
alan
cer
HTTP
Monitoring ScalingAnalytics Diagnostics