CodePark/ questions / Runtime, V8 & libuv
START PRACTICINGPRACTICE
Backend question · Node.js › Runtime, V8 & libuv

What are V8 and libuv, and what role does each play in Node.js?

V8 runs your JavaScript, and libuv gives Node its event loop and async I/O.

V8 is Google's JavaScript engine. It does these jobs:

  • It parses and compiles your JavaScript.
  • It runs the code with an interpreter and optimizing JIT compilers. A JIT (just-in-time) compiler turns code into fast machine code while the program runs.
  • It manages the heap (the memory for objects) and garbage collection.

V8 knows nothing about files, sockets or timers.

libuv is a C library. It provides the event loop and asynchronous I/O that works on every platform:

  • TCP and UDP sockets
  • timers
  • child processes
  • signals
  • the thread pool for file system work and other blocking work

Node connects the two with C++ bindings. A binding is C++ code that JavaScript can call. Here is what happens when you call fs.readFile:

  • The JavaScript API in Node's core library calls a binding.
  • The binding sends the work to libuv.
  • libuv reports that the work is done.
  • Node calls your JavaScript callback through V8.

Node also includes other libraries, such as OpenSSL for crypto and llhttp for HTTP parsing.

  • V8 compiles and runs JavaScript and handles garbage collection.
  • V8 has no I/O, so Node supplies the APIs for files, sockets and timers.
  • libuv provides the event loop, async I/O and the thread pool.
  • C++ bindings connect the JavaScript APIs to libuv and other native libraries.