How the design of Node.js makes your code fast
var data = DB.fetch("select ...") print(data) ... other stuff ...
var data = DB.fetch("select ...", function(data) { print data }) ... other stuff ...
We're taking a function call and turning the "wait" into a "wait before doing this, but continue with that…."
var data = DB.fetch("select ...", function(data) { print data }) ... other stuff ...
Wherever there is I/O.
Wherever there is I/O.
http.request(options, function (response) { render(`I received ${response.data}`) })
Wherever there is I/O.
http.request(options, function (response) { render(`I received ${response.data}`) })
redis.get(hash, (err, result) => { if (err) callback(err) console.log(`My result: ${result}`) })
Wherever there is I/O.
http.request(options, function (response) { render(`I received ${response.data}`) })
redis.get(hash, (err, result) => { if (err) callback(err) console.log(`My result: ${result}`) })
http.createServer(function (req, res) { res.sendHeader(200, {'Content-Type': 'text/plain'}) res.sendBody('Hello World\r\n') res.finish(); }).listen(8000)
Wherever there is I/O.
http.request(options, function (response) { render(`I received ${response.data}`) })
redis.get(hash, (err, result) => { if (err) callback(err) console.log(`My result: ${result}`) })
http.createServer(function (req, res) { res.sendHeader(200, {'Content-Type': 'text/plain'}) res.sendBody('Hello World\r\n') res.finish(); }).listen(8000)
fs.exists('filename', function (result) { if (result) console.log('filename exists!') })
Wherever there is I/O.
http.request(options, function (response) { render(`I received ${response.data}`) })
redis.get(hash, (err, result) => { if (err) callback(err) console.log(`My result: ${result}`) })
http.createServer(function (req, res) { res.sendHeader(200, {'Content-Type': 'text/plain'}) res.sendBody('Hello World\r\n') res.finish(); }).listen(8000)
fs.exists('filename', function (result) { if (result) console.log('filename exists!') })
var i = rl.createInterface(process.sdtin, process.stdout, null) i.question('What do you think of this presentation?', function (answer) { console.log('Thank you for your valuable feedback.') i.close() process.stdin.destroy() })
How is this different from threading? Or multiprocessing?
OS level support for evented I/O
OS level support for evented I/O, a fundamentally different mechanism.
select()
JS:
a + b
Assembler:
mov rax, a mov rbx, b call AddValues
JS:
a + b
Assembler:
mov rax, a mov rbx, b call AddValues
Are they integers? Floats? Pointers to strings? AddValues will go through the work to figure that out.
mov rax, a mov rbx, b call AddValues
mov rax, a mov rbx, b add rax, rbx
..assuming we know they are integers…huge speedup.
candidate % this.primes[i] == 0
push [ebp+0x8] mov eax,[ebp+0xc] mov edx,eax mov ecx,0x50b155dd call LoadIC_Initialize ;; this.primes push eax mov eax,[ebp+0xf4] pop edx mov ecx,eax call KeyedLoadIC_Initialize ;; this.primes[i] pop edx call BinaryOpIC_Initialize Mod ;; candidate % this.primes[i]
candidate % this.primes[i] == 0
push [ebp+0x8] mov eax,[ebp+0xc] mov edx,eax mov ecx,0x50b155dd call LoadIC_Initialize ;; this.primes push eax mov eax,[ebp+0xf4] pop edx mov ecx,eax call KeyedLoadIC_Initialize ;; this.primes[i] pop edx call BinaryOpIC_Initialize Mod ;; candidate % this.primes[i]
cmp [edi+0xff],0x4920d181 ;; Is this a Primes object? jnz 0x2a90a03c mov eax,[edi+0xf] ;; Fetch this.primes test eax,0x1 ;; Is primes a SMI ? jz 0x2a90a050 cmp [eax+0xff],0x4920b001 ;; Is primes hidden class a packed SMI array? mov ebx,[eax+0x7] mov esi,[eax+0xb] ;; Load array length sar esi,1 ;; Convert SMI length to int32 cmp ecx,esi ;; Check array bounds jnc 0x2a90a06e mov esi,[ebx+ecx*4+0x7] ;; Load element sar esi,1 ;; Convert SMI element to int32 test esi,esi ;; mod (int32) jz 0x2a90a078 ... cdq idiv esi
node --allow-natives-syntax
function printStatus(fn) { switch(%GetOptimizationStatus(fn)) { case 1: console.log("Function is optimized"); break case 2: console.log("Function is not optimized"); break case 3: console.log("Function is always optimized"); break case 4: console.log("Function is never optimized"); break case 6: console.log("Function is maybe deoptimized"); break case 7: console.log("Function is optimized by TurboFan"); break default: console.log("Unknown optimization status"); break } } printStatus(myFunction) %OptimizeFunctionOnNextCall(myFunction) myFunction()
node --trace_opt --trace_deopt
Function is not optimized [compiling method 0x3730d8083cb9 <JS Function myFunction (SharedFunctionInfo 0x3dcabc53d291)> using TurboFan] [optimizing 0x3730d8083cb9 <JS Function myFunction (SharedFunctionInfo 0x3dcabc53d291)> - took 1.606, 0.000, 0.000 ms] Function is optimized by TurboFan
Things not optimized (right now):
Things likely never optimizable:
Also: for speed, keep numbers under 31 bits!
only use: