Internals
ECMA 262 spec lists following characteristics of JS:
- High-level
- Dynamic
- Multi Paradigm
- Prototype based
Interpreter / JIT Compiler
JS is primarily an interpreted language.
But ECMA spec doesn't mention any implementation details on interpreter -> Implementation details are up to the browser vendors. Two most popular implementations are Mozilla's Spider Monkey and Chrome's V8 engine. Both differ slightly but both of them implement JIT Compilation.
Runtime
JS runtime is any engine that interprets JS code. It can be part of browser or any other runtime.
The defining feature of JS runtime is that its single-threaded.
Event-loop
Even though JS is single-threaded. It finds a way to work around this limitation using event loop.
- Any callbacks,
setTimeout
etc are added to a task queue which the event loop processes after current execution is over. - Promises are added to a 'microtask queue' which has higher priority.
Node
NodeJS is based on Chrome's lang.js.v8. Initially, lang.js was intended for use in browser. In the browser, V8 was the compiler for JS. The creator of nodeJS simply made a runtime on top of this V8 engine.
Even though it is single-threaded, nodeJS still allows execution.synchronicity.async execution via paradigm.event driven architecture. It uses the paradigm.event driven.main loop or event loop to stay in the memory, while spinning up seperate worker threads for blocking operations. The threads are not created on-demand, but rather fetched from a arch.pattern.concurrency.thread pool.
Non-blocking operations are executed in the main execution itself. The blocking operations are moved to an event queue.
Flow of a request coming to a node JS server
Bun
-
Fast (supposably)
-
Fixes interoperability problem of CommonJS and ES lang.js.org.modules