Compute a backoff delay with full jitter

Double the window on every attempt, cap it, then pick a point inside it.

JavaScript Version 1.0.0 · MIT · by Oveyon · .md

compute-an-exponential-backoff-delay.js
function backoffMs({ attempt, baseMs, capMs, random }) { return Math.round(random * Math.min(capMs, baseMs * 2 ** attempt)); }

Input & expected output

Input

{
  "attempt": 3,
  "baseMs": 500,
  "capMs": 30000,
  "random": 0.5
}

Expected output

2000

The edge case

Input

{
  "attempt": 20,
  "baseMs": 500,
  "capMs": 30000,
  "random": 0.999
}

Expected output

29970

Know the limits

  • random is an input so the result is testable; pass Math.random() in production. Honor a Retry-After header when the server sends one.
  • Every attempt must reuse the same idempotency key: Oveyon deduplicates a saved key for 24 hours, so a late retry does not send twice.

The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.

Source & maintenance

Written by Oveyon for this edition of 1loc.dev; it is not part of the original collection. Copyright (c) 2026 Oveyon. MIT licensed.

Its file in the public edition: contents/compute-an-exponential-backoff-delay.mdx in akamind/1loc, commit 0a77f131b22d. The function above is the implementation the lab runs and the tests exercise; the test suite checks that it is the code that file carries. Read the license.

Local fixture scope · Node.js 22.23.2 · reviewed October 28, 2026. No external service is required.

Keep exploring

Type two or more characters. Try “duplicate”, “webhook” or “retry”.