node.js

Post on 04-Jul-2015

1.296 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

An overview of how Node.JS works

TRANSCRIPT

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

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

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

Wait for Connection

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

“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

(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)

“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

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

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

top related