Node JS란 무엇인가?
JavaScript가 백엔드 역할을 수행할 수 있도록 하게 하는 런타임? NodeJs는 V8과 libuv를 V8 - 오픈소스 자바스크립트 엔진, libuv - c++ 오픈소스 시스템, Node가 OS의 파일시스템, 네트워킹, 컨커런시 등에 접근할 수 있게 함 Node.js 중간에 끼고 사용하는 이유?
- JS와 C++를 잇는 가교 역할을 하고, series of wrapper 이다.
Node.js가 wrapper라는 걸 알수있는 코드 여기서 보면 pbkdf2에 대한 실제 구현은 없고 _pbfdf2를 호출한다
_pbfdf2 는 PBKDF2를 호출하는데
process.binding()을 통해 c++랑 join 한다
- V8을 통해서 JS값을 C++ 값으로 바꿔준다
- libuv는 concurrency 등과같은 처리를 하는데 사용이된다.
Event Loop
NodeJs로 프로그램을 작성하면 One Thread를 생성해서 EventLoop라는 걸 만들고 코드실행
이벤트루프가 하는 일 수도코드
// node myfile.js
const pendingTimers = []
const pendingOSTasks = []
const pendingOperations = []
myFile.runContents();
function shouldContinue(){
// check one: Any pending setTimeout, setInterval, setImmediate?
// check two: Any pending OS tasks? (listening to port)
// check three: Any pending long running operations? (like fs module)
return pendingTimers.length || pendingOSTasks.length || pendingOperations.length
}
// Entire body executes in one 'tick'
while (shouldContinue()) {
// 1.Node looks at pending Timers and sees if any functions are ready to be called. setTimeout, setInterval
// 2. Node looks at pending OS tasks
// 3. Pause execution. Continues whe...
// - a new pendingOSTask
// - a new pendingOperation
// - a timer is about to complete
// 4. Look at pendingTimers
// 5. Handle any 'close' event
}
// exit back to terminal
예를들어 pbkdf2 같은거 실행해보면 (2개 이상) 거의 동일한 시간에 실행되는걸 알 수 있다.
몇개의 Standard library call 같은 경우에는, libuv가 expensive한 calculation을 결정해서 eventloop 바깥인, Thread Pool에서 진행된다.