Compute a backoff delay with full jitter
Double the window on every attempt, cap it, then pick a point inside it.
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
2000The edge case
Input
{
"attempt": 20,
"baseMs": 500,
"capMs": 30000,
"random": 0.999
}Expected output
29970Know 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.
The function above is the implementation the lab runs and the tests exercise. Read the license.
Keep exploring
- Convert an email domain to punycode — Keep the local part and convert an international domain to its ASCII xn-- form.
- Get the domain of an email address — Take the part after the last @ and lowercase it, for domain checks and grouping.
- Mask an email address for logs — Keep the first character and the domain; hide the rest of the local part.