node.js

14

Upload: rtigger

Post on 04-Jul-2015

1.296 views

Category:

Technology


0 download

DESCRIPTION

An overview of how Node.JS works

TRANSCRIPT

Page 1: Node.js
Page 2: Node.js

Interpreted using Google’s V8 JavaScript engine (same one they use in Chrome)

Provides some core libraries (network, IO, console, etc.)

Supports extensions / modules

Is both a runtime environment and a library

Page 3: Node.js

It’s not browser / document JavaScript

It’s not jQuery

It’s not PHP/Ruby/ASP.NET/other server-side language

It’s not Apache / IIS

Page 4: Node.js

Functions are Objects◦ Passed as arguments into other functions

◦ Similar to Lambdas / Anonymous Methods in .NET

◦ Ajax.oncomplete = function(result) {

document.write(result);

};

◦ $.ajax(function(result) { document.write(result); });

Callbacks EVERYWHERE

Page 5: Node.js

Wait for Connection

Page 6: Node.js

Waiting for expensive operations is stupid

Single thread event loop◦ Your code is single-threaded, so you don’t have to

deal with any concurrency / shared state / other multi-threaded problems

◦ Expensive “back-end” operations are spawned as separate processes and trigger events and/or call back to your code when they’re finished

Page 7: Node.js

“Everything runs in parallel, except your code”

Go read this file

Go listen for this message

Go download this image

K, I’m done Waiting

Page 8: Node.js
Page 9: Node.js
Page 10: Node.js

(Almost) All interaction is done via events and callbacks

If your code relies on multiple asyncoperations to complete, you’ll have to do a bit of call management / abstraction

All of “your code” should return very quickly, or create itself as an async object (via EventEmitter)

Page 11: Node.js

“parallel” IO execution allows you to make expensive queries at the same time, increasing efficiency

One language to rule them all – server side and client side code for the same web app

V8 is one of the fastest script interpreters on the planet

Ridiculously fast benchmarks◦ 600% faster than apache

Page 12: Node.js

Node Apache / PHP

Failed Requests 147 879

Requests/second

4725.43

823.38

Total ms/request

211.621

1214.510

Processing time (ms)/request

40 565

Total time (s) 21.162 121.451

100,000 requests1,000 concurrent

Page 13: Node.js

Uses more CPU and RAM on server (only a little more, but more nonetheless)

Can get complicated if doing more than one async call per request

Lots of node-specific language constructs◦ require, exports, etc.

Uses JavaScript, which in itself has a lot of quirks and fallacies