Repository navigation
Description
Summary
Code-quality audit of ./src (Grade A, score 99/100) found a single low-severity issue: a copy-paste block of 4+ byte-identical lines in src/lru.js. The insertion logic in setWithEvicted() and set() is duplicated, and the iteration logic in entries()/values()/toJSON() is near-duplicated. This is a refactor target, not a defect — no behavior is broken.
Motivation
The setWithEvicted() method (an internal optimization that avoids the full set() path for existing keys) and the public set() method share an identical insertion block. Keeping two copies in sync is a maintenance hazard: a fix or change to the insertion logic must be applied in both places, and a divergence would silently break LRU ordering or TTL handling. Extracting a shared helper removes the duplication and gives the insertion path a single source of truth.
Proposed Solution
Extract the duplicated insertion block into a private helper (e.g. #insertItem(key, value)) that both set() and setWithEvicted() call. Apply the same treatment to the shared iteration logic in entries(), values(), and toJSON() where the traversal-and-skip-expired pattern repeats. Preserve the existing 100% line coverage and the O(1) operation guarantees.
Audit Findings (for this issue)
| Rule | File | Severity | Summary |
|---|---|---|---|
| QUAL-003 | src/lru.js | low | Copy-paste block of 4+ byte-identical lines: the insertion block in setWithEvicted() (lines 362-376) is byte-identical to the one in set() (lines 410-424); the traversal-and-skip-expired pattern in entries()/values()/toJSON() is near-duplicated. |
Details
src/lru.jslines 362-376 vs 410-424 — theitem = this.items[key] = { ... }insertion block, theif (++this.size === 1)first/last wiring, andthis.last = itemare byte-identical acrosssetWithEvicted()andset().entries()/values()/toJSON()— the "walk the linked list, skip expired items, collect" pattern repeats. A shared traversal helper would consolidate it.- No security, bug, or performance findings. No critical/high/medium issues.
Fix Steps
- Locate the duplication — Open
src/lru.jsand compare the insertion blocks at lines 362-376 (setWithEvicted) and 410-424 (set); confirm they are byte-identical. - Extract a helper — Add a private method
#insertItem(key, value)that performs thethis.items[key] = { ... }assignment, the++this.size === 1first/last wiring, andthis.last = item. - Update call sites — Replace the duplicated blocks in both
set()andsetWithEvicted()with a call to#insertItem(key, value). - Consolidate iteration — Extract the shared "walk list, skip expired, collect" pattern used by
entries(),values(), andtoJSON()into a helper where it reduces duplication without changing behavior. - Verify — Run
npm run testandnpm run coverage; confirm all 173 tests pass and 100% line coverage is maintained.
Environment
- OS: Linux 7.0.14-linuxkit
- Node.js: v26.13.1
- madz version: 13.1.1
- LLM provider: Unknown — user to confirm
Activity
avoidwork commentedon Nov 25, 2026
Fixed in #548.