Performance: Try-catch for synchronous errors
MEDIUM IMPACT
This affects how quickly synchronous errors are caught and handled without blocking the event loop or causing unexpected crashes.
const fs = require('fs'); function readFileSync(path) { try { const data = fs.readFileSync(path, 'utf8'); return JSON.parse(data); } catch (error) { console.error('Failed to read or parse file:', error); return null; } } const result = readFileSync('data.json');
const fs = require('fs'); function readFileSync(path) { const data = fs.readFileSync(path, 'utf8'); return JSON.parse(data); } const result = readFileSync('data.json');
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No try-catch on sync errors | N/A | N/A | N/A | [X] Bad - crashes block event loop |
| Try-catch around sync errors | N/A | N/A | N/A | [OK] Good - prevents crashes with minimal overhead |