Run-time of Node.js
V8 JavaScript Engine
Gary
2014/2/26
Outline
• Overview of Node.js
• V8 JavaScript Engine
• Fast property access
• Dynamic machine code generation
• Efficient Garbage Collection
• Hello World Demo
Overview of Node.js
• Node.js
• Node.js was created by Ryan Dahl starting in 2009.
• Dahl was inspired to create Node.js after seeing a file upload progress bar on
Flickr.
• The browser did not know how much of the file had been uploaded and had
to query the web server.
• Dahl wanted an easier way.
Overview of Node.js
• Old School JavaScript
JavaScript Engine
Web Browser
Operation System
Hardware
Overview of Node.js
• Without Browser
JavaScript Engine
Operation System
Hardware
Overview of Node.js
• With Node.js
JavaScript Engine
Node.js API
Operation System
Hardware
Overview of Node.js
• More third Party Modules
JavaScript Engine
Node.js API
Operation System
Hardware
ModuleD
ModuleC
ModuleA
ModuleB
Overview of Node.js
• More Supports
JavaScript Engine
Node.js API
Operation System
Hardware
ModuleD
ModuleC
ModuleA
ModuleBCloud
Service
Database
(MySQL/PostgreSQL/
MongoDB…etc)
Libraries
Java
C/C++
Objective C
Overview of Node.js
• Node.js
• A platform built on Chrome’s JavaScript runtime for easily building fast,
scalable network applications.
• Uses an event-driven, non-blocking I/O model that makes it lightweight and
efficient
• Perfect for data-intensive real-time applications that run across distributed
devices
• Contains a built-in HTTP server library
• Making it possible to run a web server without the use of external software,
such as Apache or Lighttpd
• Allowing more control of how the web server works
Overview of Node.js
• There are four building blocks that constitute Node.
• Node encapsulates libuv to handle asynchronous events
• Libuv is what abstracts away all of the underlying network and file system functionality
on both Windows and POSIX-based systems like Linux, Mac OSX and Unix.
• Google’s V8 to provide a run-time for JavaScript.
• The core functionality of Node, modules like Assert, HTTP, Crypto etc, reside
in a core library written in JavaScript.
• The Node bindings, written in C++, provide the glue connecting these
technologies to each other and to the operating system.
Overview of Node.js
• Node.js architecture
Node bindings
V8
Node Standard library
libuv
JavaScript
C/C++
V8 JavaScript Engine
• The V8 JavaScript Engine is an open source JavaScript engine
developed by Google for Google Chrome web browser.
• The first version of the V8 engine was released at the same time as
the first version of Chrome, September 2, 2008
• There are three key areas to V8’s performance
• Fast property access
• Dynamic machine code generation
• Efficient Garbage Collection
V8 JavaScript Engine
Note: Google is forking webkit to create a
new rendering engine, called Blink, for
chrome and opera, at April 2013
V8 – Fast Property Access
• JavaScript is a dynamic programming language
• Properties can be added to, and deleted from, objects on the fly.
• This means an object's properties are likely to change.
• Most JavaScript engines use a dictionary-like data structure as storage for object
properties - each property access requires a dynamic lookup to resolve the
property's location in memory.
• This approach makes accessing properties in JavaScript typically much slower
than accessing instance variables in programming languages like Java and
Smalltalk.
• In these languages, instance variables are located at fixed offsets determined by
the compiler due to the fixed object layout defined by the object's class.
• Access is simply a matter of a memory load or store, often requiring only a single
instruction.
V8 – Fast Property Access
• Difference between dynamic programming language and static
programming language
V8 – Fast Property Access
V8 – Fast Property Access
• To reduce the time required to access JavaScript properties, V8 does
not use dynamic lookup to access properties.
• Instead, V8 dynamically creates hidden classes behind the scenes.
• In V8, an object changes its hidden class when a new property is
added.
V8 – Fast Property Access
• When new Point(x, y) is executed a new Point object is created.
• When V8 does this for the first time, V8 creates an initial hidden class
of Point, called C0 in this example.
• As the object does not yet have any properties defined the initial class
is empty.
• At this stage the Point object's hidden class is C0.
function Point(x, y){
this.x = x;
this.y = y;
}
V8 – Fast Property Access
• Executing the first statement in Point (this.x = x;) creates a new
property, x, in the Point object.
• In this case, V8:
V8 – Fast Property Access
• Executing the second statement in Point (this.y = y;) creates a new
property, y, in the Point object.
• In this case, V8:
V8 – Fast Property Access
• It might seem inefficient to create a new hidden class whenever a
property is added.
• However, because of the class transitions the hidden classes can be
reused.
• The next time a new Point is created no new hidden classes are
created, instead the new Point object shares the classes with the first
Point object.
V8 – Fast Property Access
• For example, if another Point object is created:
• initially the Point object has no properties so the newly created object refers
to the intial class C0.
• when property x is added, V8 follows the hidden class transition from C0 to
C1 and writes the value of x at the offset specified by C1.
• when property y is added, V8 follows the hidden class transition from C1 to
C2 and writes the value of y at the offset specified by C2.
V8 – Dynamic Machine Code Generation
• V8 compiles JavaScript source code directly into machine code when
it is first executed.
• There are no intermediate byte codes, no interpreter.
• For example, the JavaScript code to access property x from a Point
object is:
• In V8, the machine code generated for accessing x is:
point.x
# ebx = the point object
cmp [ebx,<hidden class offset>],<cached hidden class>
jne <inline cache miss>
mov eax,[ebx, <cached x offset>]
V8 – Dynamic Machine Code Generation
V8 – Dynamic Machine Code Generation
V8 – Dynamic Machine Code Generation
V8 – Efficient Garbage Collection
• Stop-the-world
• Garbage collectors completely halt execution of the program to run a
collection cycle, thus guaranteeing that new objects are not allocated and
objects do not suddenly become unreachable while the collector is running.
• Accurate
• Always knows exactly where all objects and pointers are in memory. This
avoids falsely identifying objects as pointers which can result in memory leaks.
V8 – Efficient Garbage Collection
• Generational
• Each objects is divided into generations (based on the hypothesis about the
object) and places them into a memory heap for a particular generation.
When that memory heap is filled up, the GC cycle begins, and those objects
that still references are moved to another memory heap and fresh objects are
added.
Hello World Demo
var ip = "127.0.0.1";
var port = 1337;
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(port, ip);
console.log("Server running at http://" + ip + ":" + port);
Conclusion
• More recently, following the arrival of AJAX, JavaScript has become a
central technology for implementing web-based applications such as
GMail.
• JavaScript programs have grown from a few lines to several hundred
kilobytes of source code. While JavaScript is very efficient in doing the
things it was designed to do, performance has become a limiting
factor to further development of web-based JavaScript applications.
• V8 is a new JavaScript engine specifically designed for fast execution
of large JavaScript applications.

Run-time of Node.js : V8 JavaScript Engine

  • 1.
    Run-time of Node.js V8JavaScript Engine Gary 2014/2/26
  • 2.
    Outline • Overview ofNode.js • V8 JavaScript Engine • Fast property access • Dynamic machine code generation • Efficient Garbage Collection • Hello World Demo
  • 3.
    Overview of Node.js •Node.js • Node.js was created by Ryan Dahl starting in 2009. • Dahl was inspired to create Node.js after seeing a file upload progress bar on Flickr. • The browser did not know how much of the file had been uploaded and had to query the web server. • Dahl wanted an easier way.
  • 4.
    Overview of Node.js •Old School JavaScript JavaScript Engine Web Browser Operation System Hardware
  • 5.
    Overview of Node.js •Without Browser JavaScript Engine Operation System Hardware
  • 6.
    Overview of Node.js •With Node.js JavaScript Engine Node.js API Operation System Hardware
  • 7.
    Overview of Node.js •More third Party Modules JavaScript Engine Node.js API Operation System Hardware ModuleD ModuleC ModuleA ModuleB
  • 8.
    Overview of Node.js •More Supports JavaScript Engine Node.js API Operation System Hardware ModuleD ModuleC ModuleA ModuleBCloud Service Database (MySQL/PostgreSQL/ MongoDB…etc) Libraries Java C/C++ Objective C
  • 9.
    Overview of Node.js •Node.js • A platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. • Uses an event-driven, non-blocking I/O model that makes it lightweight and efficient • Perfect for data-intensive real-time applications that run across distributed devices • Contains a built-in HTTP server library • Making it possible to run a web server without the use of external software, such as Apache or Lighttpd • Allowing more control of how the web server works
  • 10.
    Overview of Node.js •There are four building blocks that constitute Node. • Node encapsulates libuv to handle asynchronous events • Libuv is what abstracts away all of the underlying network and file system functionality on both Windows and POSIX-based systems like Linux, Mac OSX and Unix. • Google’s V8 to provide a run-time for JavaScript. • The core functionality of Node, modules like Assert, HTTP, Crypto etc, reside in a core library written in JavaScript. • The Node bindings, written in C++, provide the glue connecting these technologies to each other and to the operating system.
  • 11.
    Overview of Node.js •Node.js architecture Node bindings V8 Node Standard library libuv JavaScript C/C++
  • 12.
    V8 JavaScript Engine •The V8 JavaScript Engine is an open source JavaScript engine developed by Google for Google Chrome web browser. • The first version of the V8 engine was released at the same time as the first version of Chrome, September 2, 2008 • There are three key areas to V8’s performance • Fast property access • Dynamic machine code generation • Efficient Garbage Collection
  • 13.
    V8 JavaScript Engine Note:Google is forking webkit to create a new rendering engine, called Blink, for chrome and opera, at April 2013
  • 14.
    V8 – FastProperty Access • JavaScript is a dynamic programming language • Properties can be added to, and deleted from, objects on the fly. • This means an object's properties are likely to change. • Most JavaScript engines use a dictionary-like data structure as storage for object properties - each property access requires a dynamic lookup to resolve the property's location in memory. • This approach makes accessing properties in JavaScript typically much slower than accessing instance variables in programming languages like Java and Smalltalk. • In these languages, instance variables are located at fixed offsets determined by the compiler due to the fixed object layout defined by the object's class. • Access is simply a matter of a memory load or store, often requiring only a single instruction.
  • 15.
    V8 – FastProperty Access • Difference between dynamic programming language and static programming language
  • 16.
    V8 – FastProperty Access
  • 17.
    V8 – FastProperty Access • To reduce the time required to access JavaScript properties, V8 does not use dynamic lookup to access properties. • Instead, V8 dynamically creates hidden classes behind the scenes. • In V8, an object changes its hidden class when a new property is added.
  • 18.
    V8 – FastProperty Access • When new Point(x, y) is executed a new Point object is created. • When V8 does this for the first time, V8 creates an initial hidden class of Point, called C0 in this example. • As the object does not yet have any properties defined the initial class is empty. • At this stage the Point object's hidden class is C0. function Point(x, y){ this.x = x; this.y = y; }
  • 19.
    V8 – FastProperty Access • Executing the first statement in Point (this.x = x;) creates a new property, x, in the Point object. • In this case, V8:
  • 20.
    V8 – FastProperty Access • Executing the second statement in Point (this.y = y;) creates a new property, y, in the Point object. • In this case, V8:
  • 21.
    V8 – FastProperty Access • It might seem inefficient to create a new hidden class whenever a property is added. • However, because of the class transitions the hidden classes can be reused. • The next time a new Point is created no new hidden classes are created, instead the new Point object shares the classes with the first Point object.
  • 22.
    V8 – FastProperty Access • For example, if another Point object is created: • initially the Point object has no properties so the newly created object refers to the intial class C0. • when property x is added, V8 follows the hidden class transition from C0 to C1 and writes the value of x at the offset specified by C1. • when property y is added, V8 follows the hidden class transition from C1 to C2 and writes the value of y at the offset specified by C2.
  • 23.
    V8 – DynamicMachine Code Generation • V8 compiles JavaScript source code directly into machine code when it is first executed. • There are no intermediate byte codes, no interpreter. • For example, the JavaScript code to access property x from a Point object is: • In V8, the machine code generated for accessing x is: point.x # ebx = the point object cmp [ebx,<hidden class offset>],<cached hidden class> jne <inline cache miss> mov eax,[ebx, <cached x offset>]
  • 24.
    V8 – DynamicMachine Code Generation
  • 25.
    V8 – DynamicMachine Code Generation
  • 26.
    V8 – DynamicMachine Code Generation
  • 27.
    V8 – EfficientGarbage Collection • Stop-the-world • Garbage collectors completely halt execution of the program to run a collection cycle, thus guaranteeing that new objects are not allocated and objects do not suddenly become unreachable while the collector is running. • Accurate • Always knows exactly where all objects and pointers are in memory. This avoids falsely identifying objects as pointers which can result in memory leaks.
  • 28.
    V8 – EfficientGarbage Collection • Generational • Each objects is divided into generations (based on the hypothesis about the object) and places them into a memory heap for a particular generation. When that memory heap is filled up, the GC cycle begins, and those objects that still references are moved to another memory heap and fresh objects are added.
  • 29.
    Hello World Demo varip = "127.0.0.1"; var port = 1337; var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(port, ip); console.log("Server running at http://" + ip + ":" + port);
  • 30.
    Conclusion • More recently,following the arrival of AJAX, JavaScript has become a central technology for implementing web-based applications such as GMail. • JavaScript programs have grown from a few lines to several hundred kilobytes of source code. While JavaScript is very efficient in doing the things it was designed to do, performance has become a limiting factor to further development of web-based JavaScript applications. • V8 is a new JavaScript engine specifically designed for fast execution of large JavaScript applications.