Critical Thinking in Node.js
Morgan Cheng
What is Node.JS?
History
• 2009, Ryan Dahl published Node.js
• Bla Bla Bla
• https://blog.risingstack.com/history-of-node-js/
io.js & node.js
Why choose JavaScript?
JavaScript is clean in I/O
Asynchronous Programming
Scale Out? Scale Up?
Single Thread?
IT IS A JOKE
Event Loop
"Everything runs in parallel except your code"
How about add Threading in JavaScript?
Promise vs CPS
CPS
• Easy to callback-hell
• High Performant
Promise
• Seems winning
Rocks for I/O intensive job
Sucks for CPU intensive job
But, Node.js might still fail your Expectation
Watch Out
•Sync API
•Loop
•Recursion
•GC
It Could Freeze You!
Garbage Collection
Real Single Threading
This is Node.js
Happy?
Any Question?

Critical thinking in Node.js

Editor's Notes

  • #2 This presentation is to help you be critical in choosing Node.js as tech solution, not to boost Node.js
  • #3 Node.js can be anywhere, except space shuttle.
  • #5 io.js a fork of original node.js, because some people is not satisfied by slow steps of node evolution. When these two merge, node.js is still 0.xxx, but io.js has reached v3.3.1. So, the new version of node.js starts with v4. This story sounds similar. Yes, same happens for HTML5. LTS(v4, v6) vs Current(v5) LTS focus on stability and security. Current focus on new features.
  • #6 Node has only very basic and raw functionality It is NPM that makes it powerful.
  • #7 JavaScript is asynchronous, by default. JavaScript had no existing IO libraries so we could start clean with non-blocking IO. Example. Crawler in .NET
  • #9 Actually, Universal JavaScript is the biggest incentive. Frontend Engineers are most loyal language user, since they have not much choice.
  • #10 It is difficult, but not impossible.
  • #11 Node.js helps scale-up, not scale-out. Actually, it doesn’t implement share-nothing-architecture like PHP. As a result, it may suffer some scalability problems.
  • #13 http://softwareengineeringdaily.com/2015/08/02/how-does-node-js-work-asynchronously-without-multithreading/ It is a hoax that Node process has only single thread. There are multiple threads, but your code always run on single thread. Check it out: ps –M {pid} Single Threaded brings pros: No race condition; Better performance since thread switching is not necessary.
  • #14 In Node.js everything runs in parallel, except your code. What this means is that all I/O code that you write in Node.js is non-blocking, while (conversely) all non-I/O code that you write in Node.js is blocking.
  • #16 Dougalus Crockford’s response: never!!! JavaScript is application language, not system language. https://www.youtube.com/watch?v=QgwSUtYSUqA
  • #17 https://blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-functional-nodes-biggest-missed-opportunity/ Ryan Dahl decides to take CPS way.
  • #21 Example. Read cson config in view So, every server application should have two phase: Init phase; Execution Phase.
  • #22 Example: Read cson from loop in view. Example: _.merge Example: Plurk
  • #24 Node.js GC is actually google v8 js engine GC. So, it runs in single thread. Example, plurk moves away from node.js