Skip to content

feat: deduplicate copy-paste blocks in src/lru.js #547

In review

Description

@avoidwork

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.js lines 362-376 vs 410-424 — the item = this.items[key] = { ... } insertion block, the if (++this.size === 1) first/last wiring, and this.last = item are byte-identical across setWithEvicted() and set().
  • 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

  1. Locate the duplication — Open src/lru.js and compare the insertion blocks at lines 362-376 (setWithEvicted) and 410-424 (set); confirm they are byte-identical.
  2. Extract a helper — Add a private method #insertItem(key, value) that performs the this.items[key] = { ... } assignment, the ++this.size === 1 first/last wiring, and this.last = item.
  3. Update call sites — Replace the duplicated blocks in both set() and setWithEvicted() with a call to #insertItem(key, value).
  4. Consolidate iteration — Extract the shared "walk list, skip expired, collect" pattern used by entries(), values(), and toJSON() into a helper where it reduces duplication without changing behavior.
  5. Verify — Run npm run test and npm 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

  1. added
    in progress
    and removed
    in progress
    on Nov 24, 2026
  2. avoidwork commented on Nov 25, 2026

    @avoidwork
    OwnerAuthor

    Fixed in #548.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedfeaturein progress

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions