</>
⌕
Junior #nodejs#v8#libuv#runtime

Node.js là gì? Vai trò của V8 và libuv

⚡ Trả lời nhanh

Node.js là runtime JavaScript chạy ngoài trình duyệt. V8 biên dịch và thực thi JS; libuv cung cấp event loop, thread pool và I/O bất đồng bộ đa nền tảng. Node bind hai thứ này lại và thêm API như fs, http, net.

Thành phầnVai trò
V8Engine của Chrome: parse, JIT compile JS, quản lý heap và garbage collection
libuvEvent loop, thread pool (mặc định 4 thread), socket, file system, DNS, timer
Node bindings (C++)Cầu nối giữa JS và V8/libuv/OpenSSL/zlib…
Core modulesfs, http, net, crypto, stream, path…

JS của bạn chạy trên một thread chính. Các tác vụ nặng I/O được giao cho OS (socket) hoặc thread pool của libuv (fs, dns.lookup, crypto, zlib), xong thì callback được đưa lại event loop.

const fs = require('fs');
fs.readFile(__filename, () => console.log('đọc xong'));  // libuv thread pool
console.log('chạy trước');
🃏 Ôn bằng flashcard