Promise cleanup: .finally() vs try/finally

Benchmark created on


Description

Comparing performance between using .finally() for cleanup vs try/finally pattern in Promise chains

Setup

let activePromises = new Set();
let mockProcessor = async (item) => {
  await new Promise(resolve => setTimeout(resolve, 0)); // Simulate async work
  return item * 2;
};

Test runner

Ready to run.

Testing in
TestOps/sec
.finally() pattern
async function finallyPattern(item) {
  const promise = (async () => {
    await mockProcessor(item);
  })().finally(() => {
    activePromises.delete(promise);
  });
  activePromises.add(promise);
  return promise;
}

finallyPattern(Math.random() * 1000);
ready
try/finally pattern
async function tryFinallyPattern(item) {
  const promise = (async () => {
    try {
      await mockProcessor(item);
    } finally {
      activePromises.delete(promise);
    }
  })();
  activePromises.add(promise);
  return promise;
}

tryFinallyPattern(Math.random() * 1000);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.