Skip to content

Instantly share code, notes, and snippets.

@tarekahsan709
Last active January 18, 2019 03:23
Show Gist options
  • Save tarekahsan709/a882be74dbdd8fb38787c3a06b0f7ddf to your computer and use it in GitHub Desktop.
Save tarekahsan709/a882be74dbdd8fb38787c3a06b0f7ddf to your computer and use it in GitHub Desktop.

Why Node js ?

We were developing a real time application, which need a persistent connection with ther server. Nodejs does not create a new connection for every request. One thread per connection (blocking I/O or non-blocking I/O) requires more memory resources (also kernel memory) is a disadvantage. And every additional thread means more work for the scheduler.

How the single threaded non blocking IO model works in Node.js

The program code running in nodejs thread is excuted synchronously but Whenever an I/O request come node queue the request to the event loop along with a callback function. And Libev is the event loop which actually runs internally in node.js to perform simple event loop operations. LibUv performs, mantains and manages all the io and events in the event pool. ( in case of libeio threadpool ). So outside of the main thread, libev and libeio handle it in the thread pool and libev provide the interaction with the main loop. https://stackoverflow.com/questions/10680601/nodejs-event-loop

Why node js single thread non blocking IO better then multithread ?

Suppose for 100 parallel requests, the server will need 100 processes or threads to handle them. This means if 100 processes are currently waiting for a database result and the next request comes in a new process will be created. Creating and maintaining processes consume CPU and memory.

Node takes a different approach by serving all requests from one single thread. The program code running in this thread is still executed synchronously but every time a system call takes place it will be delegated to the event loop along with a callback function.

Our main process will not be put to sleep and will continue serving other requests. As soon as the previous system call is completed, the event loop will execute the callback passed. This callback will usually deal with the result returned and continue the program flow.

References :

http://khan.io/2015/02/25/the-event-loop-and-non-blocking-io-in-node-js/ https://stackoverflow.com/questions/8546273/is-non-blocking-i-o-really-faster-than-multi-threaded-blocking-i-o-how https://stackoverflow.com/questions/21485920/single-threaded-event-loop-vs-multi-threaded-non-blocking-worker-in-node-js https://stackoverflow.com/questions/21485920/single-threaded-event-loop-vs-multi-threaded-non-blocking-worker-in-node-js

Misconceptio about nodejs

https://medium.com/the-node-js-collection/what-you-should-know-to-really-understand-the-node-js-event-loop-and-its-metrics-c4907b19da4c

When to use node js

https://stackoverflow.com/questions/5062614/how-to-decide-when-to-use-node-js

In which type of project we should use Node js ?

https://stackoverflow.com/questions/5062614/how-to-decide-when-to-use-node-js

What is event loop ?

https://stackoverflow.com/questions/19822668/what-exactly-is-a-node-js-event-loop-tick https://stackoverflow.com/questions/25568613/node-js-event-loop

What is object composition ?

Object composition is a way to combine simple objects or data types into more complex ones. Compositions are a critical building block of many basic data structures, including the tagged union, the linked list, and the binary tree, as well as the object used in object-oriented programming.

Composition over inheritance

What is closure in js ?

A closure is the combination of a function and the lexical environment within which that function was declared https://developer.mozilla.org/en/docs/Web/JavaScript/Closures https://nodeschool.io/

What is scope in js ?

How this work in js ?

What is promise in js & how to implement it?

A Promise object represents an operation which has produced or will eventually produce a value. Promises provide a robust way to wrap the (possibly pending) result of asynchronous work, mitigating the problem of deeply nested callbacks (known as "callback hell"). https://stackoverflow.com/documentation/javascript/231/promises#t=201708200727083480666

Difference between callback and promise ?

Promises are not callbacks. A promise represents the future result of an asynchronous operation. Of course, writing them the way you do, you get little benefit. https://stackoverflow.com/questions/22539815/arent-promises-just-callbacks

What is async wait, generator and templete string in js ?

Which node js frame work you used ?

Express a node js web application framework. http://expressjs.com/

Why Express?

We were building a web application. And Express is a node js web application framework to help and provide feature to build a web application on node. It also organize web application into an MVC architecture on the server side. It provide the capability to connect databases. Express.js basically helps you manage everything, from routes, to handling requests and views. https://stackoverflow.com/questions/12616153/what-is-express-js https://stackoverflow.com/questions/28823253/what-are-the-benefits-of-express-over-plain-node-js https://stackoverflow.com/questions/26615443/express-js-benefits-to-using-router-vs-app-use-routing

What is middleware ?

Middleware is software that allows a bunch of isolated systems or functionalities to interact. So if you have a website, and a payment system, you use middleware to hookem up.

What is middleware functions?

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

An Express application can use the following types of middleware:

Application-level middleware, Router-level middleware, Error-handling middleware, Built-in middleware, Third-party middleware http://expressjs.com/en/guide/using-middleware.html

What is next function ?

The next middleware function is commonly denoted by a variable named next.

Difference between HAPI and Express ?

OOP in js and Functional programming in js ?

Pure functions / function purity

https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076bec976

What is imperative, procedural and structural programming ?

https://softwareengineering.stackexchange.com/questions/117092/whats-the-difference-between-imperative-procedural-and-structured-programming

Immutability in js.

var str = 'foo';

alert(str.substr(1)); // oo

alert(str); // foo

On a lower level, immutability means that the memory the string is stored in will not be modified. Once you create a string "foo", some memory is allocated to store the value "foo". This memory will not be altered. If you modify the string with, say, substr(1), a new string is created and a different part of memory is allocated which will store "oo". Now you have two strings in memory, "foo" and "oo". Even if you're not going to use "foo" anymore, it'll stick around until it's garbage collected.

One reason why string operations are comparatively expensive.

https://www.sitepoint.com/immutability-javascript/

when to use prototypes ?

Articulate the difference between composition and class inheritance, or the advantages of composition

Pros and cons of monolithic vs microservice architectures.

What's The Difference Between Imperative, Procedural and Structured Programming?

https://softwareengineering.stackexchange.com/questions/117092/whats-the-difference-between-imperative-procedural-and-structured-programming

CommonJS module system.

What is function composition.

Mathmatical explanation: https://www.khanacademy.org/math/algebra2/manipulating-functions/funciton-composition/v/function-composition Example and explanation in js: https://medium.com/javascript-scene/master-the-javascript-interview-what-is-function-composition-20dfb109a1a0

What is a network-based application?

http://bpastudio.csudh.edu/fac/lpress/471/hout/apps/whatis.htm

What is Dependency Injection ?

https://www.javatpoint.com/dependency-injection-in-spring

What is serialization and deserialization

https://en.wikipedia.org/wiki/Serialization

Reference

https://medium.com/javascript-scene/10-interview-questions-every-javascript-developer-should-know-6fa6bdf5ad95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment