Performance: Why async patterns are critical in Node.js
This concept affects how fast Node.js can handle multiple tasks without blocking the server, impacting response time and throughput.
Jump into concepts and practice - no test required
const fs = require('fs/promises'); async function readFile() { const data = await fs.readFile('/file.txt'); console.log(data.toString()); } readFile();
const fs = require('fs'); const data = fs.readFileSync('/file.txt'); console.log(data.toString());
| Pattern | Event Loop Blocking | Throughput Impact | Responsiveness | Verdict |
|---|---|---|---|---|
| Synchronous blocking calls | Blocks event loop | Reduces throughput drastically | Causes input lag | [X] Bad |
| Async/await with promises | Non-blocking | Maximizes throughput | Keeps server responsive | [OK] Good |
async before the function keyword.async function myFunc() {}.async function fetchData() {
return 'data';
}
fetchData().then(console.log);
console.log('start');async function readFile() {
const data = fs.readFileSync('file.txt');
console.log(data);
}