Performance: Reading and writing buffer data
MEDIUM IMPACT
This concept affects how fast Node.js can process binary data streams and file I/O operations, impacting server responsiveness and throughput.
const fs = require('fs'); const readStream = fs.createReadStream('file.bin'); const writeStream = fs.createWriteStream('file-copy.bin'); readStream.pipe(writeStream);
const fs = require('fs'); fs.readFile('file.bin', (err, data) => { if (err) throw err; const str = data.toString('utf8'); // process string const newBuffer = Buffer.from(str, 'utf8'); fs.writeFile('file-copy.bin', newBuffer, (err) => { if (err) throw err; }); });
| Pattern | CPU Usage | Memory Usage | Event Loop Blocking | Verdict |
|---|---|---|---|---|
| Buffer to string conversion and back | High (extra encoding/decoding) | High (extra buffer copies) | Blocks event loop during conversion | [X] Bad |
| Direct buffer streaming with pipe() | Low (minimal CPU overhead) | Low (no extra copies) | Non-blocking, asynchronous | [OK] Good |