From e32229ba5bba9c6ec86d7fca200168039cd85ff2 Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Wed, 10 Dec 2025 22:45:37 +0100 Subject: [PATCH] perf(backoff): optimize linear backoff using closure - Remove lodash dependency for faster property access - Pre-calculate range in closure to avoid repeated subtraction on each call - Add early return optimization for constant values (min === max) - Use modern JS features (default parameters, arrow functions) The range calculation is moved from the hot path (next() function) to the closure scope, ensuring it's computed only once when the backoff function is created rather than on every invocation. --- lib/backoff/linear.js | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/backoff/linear.js b/lib/backoff/linear.js index 6610ce2..091e501 100644 --- a/lib/backoff/linear.js +++ b/lib/backoff/linear.js @@ -1,18 +1,11 @@ -const get = require('lodash').get; - -module.exports = function (options) { - const min = get(options, 'min', 1000); - const max = get(options, 'max', min); +module.exports = function (options = {}) { + const min = options.min || 1000; + const max = options.max || min; + const range = max - min; function next() { - return Math.floor(Math.random() * (max - min + 1) + min); + return range === 0 ? min : Math.floor(Math.random() * (range + 1) + min); } - // eslint-disable-next-line no-empty-function - function reset() {} - - return { - next, - reset, - }; + return { next, reset: () => {} }; };