Performance: Checking file existence and stats
MEDIUM IMPACT
This concept affects the responsiveness and speed of file-related operations in a Node.js application, impacting how quickly the app can proceed after checking files.
import { access, stat } from 'node:fs/promises'; async function checkFile() { try { await access('file.txt'); const stats = await stat('file.txt'); console.log(stats.size); } catch (err) { console.error(err); } } checkFile();
const fs = require('fs'); try { if (fs.existsSync('file.txt')) { const stats = fs.statSync('file.txt'); console.log(stats.size); } } catch (err) { console.error(err); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous file check | N/A | N/A | N/A | [X] Bad |
| Asynchronous file check with promises | N/A | N/A | N/A | [OK] Good |