Skip to content

Commit 1c2f199

Browse files
perf(backoff): optimize linear backoff using closure (#243)
- 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.
1 parent 139ba71 commit 1c2f199

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

lib/backoff/linear.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
const get = require('lodash').get;
2-
3-
module.exports = function (options) {
4-
const min = get(options, 'min', 1000);
5-
const max = get(options, 'max', min);
1+
module.exports = function (options = {}) {
2+
const min = options.min || 1000;
3+
const max = options.max || min;
4+
const range = max - min;
65

76
function next() {
8-
return Math.floor(Math.random() * (max - min + 1) + min);
7+
return range === 0 ? min : Math.floor(Math.random() * (range + 1) + min);
98
}
109

11-
// eslint-disable-next-line no-empty-function
12-
function reset() {}
13-
14-
return {
15-
next,
16-
reset,
17-
};
10+
return { next, reset: () => {} };
1811
};

0 commit comments

Comments
 (0)