v134 · javascript
Resource Leak Detector
Run the same scenario with manual try/finally vs using and compare leaked vs cleaned resources. When a function throws mid-way, using still disposes every resource in scope — try/finally requires remembering each one individually.
checking using keyword…
The problem: code that opens multiple resources often leaks some on the throw path.
try/finally works but only if every resource is referenced in the finally block. using r = openResource() automatically calls r[Symbol.dispose]() on exit — even if the scope throws before the resource is explicitly closed.
Run scenario
Without using (manual)
—
leaked resources
With using
—
leaked resources
Execution log
Click "Run both approaches" to start.
The difference
// Without using — easy to miss resources in finally
function processFile(path) {
const handle = openFile(path);
const lock = acquireLock(path);
const logger = openLogger();
try {
doWork(handle, lock, logger); // may throw
return result;
} finally {
handle.close(); // if lock.release() above throws, logger leaks
lock.release();
// ← forgot logger.close() !
}
}
// With using — every resource disposed automatically, LIFO
function processFile(path) {
using handle = openFile(path);
using lock = acquireLock(path);
using logger = openLogger();
doWork(handle, lock, logger); // throws? all three close
return result;
// ← Symbol.dispose called on logger, lock, handle (LIFO)
}