Zero-Copy Data Transfer
In this lesson, you’ll learn how to bypass the CPU-heavy process of moving data between system memory and application memory to transfer files over a network faster.
The Cost of Copying Data
When you normally read a file to send it over a network in Node.js, the operating system reads the data from the disk into kernel memory, then copies it into your application's memory (the V8 heap), and finally copies it back to the kernel’s network buffer to send it out. Each copy operation consumes CPU cycles and increases memory pressure.
"Zero-Copy" allows the operating system to move data directly from the file system cache to the network card buffer, skipping the stopover in your Node.js application memory entirely.
The pipe Strategy
In Node.js, the stream.pipe() method is the standard way to achieve this efficiency. When you pipe a Readable file stream directly into a Writable network stream (like an HTTP response), Node.js triggers a low-level system call (often sendfile on Linux). This tells the kernel to handle the data transfer directly, avoiding the overhead of copying bytes into the JavaScript environment.
Here is how simple it looks in practice:
const fs = require('fs');
const http = require('http');
http.createServer((req, res) => {
// We create a stream from the file
const fileStream = fs.createReadStream('large-video.mp4');
// .pipe() bridges the file directly to the network socket
// without reading the whole file into the V8 heap
fileStream.pipe(res);
}).listen(3000);
By using .pipe(), your application doesn't need to allocate large Buffers to hold the file contents. Even if you are serving a multi-gigabyte file, your application’s memory usage remains low and stable because the data never resides in the application's process memory during the transfer.
Key Takeaway: Zero-copy transfers use pipe() to instruct the kernel to move data directly from the disk to the network, eliminating unnecessary memory copying and drastically reducing CPU load.