Skip to content

Simplify toggleId to use filter instead of slice - #184

Merged
JZimz merged 1 commit into
mainfrom
claude/fervent-einstein-16j58
Apr 25, 2026
Merged

JZimz merged 1 commit into
mainfrom
claude/fervent-einstein-16j58

Conversation

@JZimz

@JZimz JZimz commented Apr 21, 2026

Copy link
Copy Markdown
Owner

The toggleId function in RomList.vue was removing items from the selection array by reconstructing it with two slice calls, which allocates three arrays per deselection. This pattern also needed inline comments to explain what was happening, which is a sign the code wasn't self-describing.

Before:

function toggleId(selections: string[], id: string): string[] {
  const idx = selections.indexOf(id);
  if (idx !== -1) {
    // Remove selection
    return [...selections.slice(0, idx), ...selections.slice(idx + 1)];
  } else {
    // Add
    return [...selections, id];
  }
}

After:

function toggleId(selections: string[], id: string): string[] {
  return selections.includes(id)
    ? selections.filter((sid) => sid !== id)
    : [...selections, id];
}

Why it's better:

  • Fewer allocations: filter does a single pass and creates one array instead of two intermediate slices plus a spread.
  • More readable: includes + filter directly expresses intent — "if it's in there, remove it; otherwise add it" — without needing comments.
  • Less code: 5 lines → 3 lines with no loss of clarity.

The behaviour is identical; this is a pure readability and minor performance improvement.

https://claude.ai/code/session_01XFx8U5RA5zMsnNwKB9inMZ

Replaces the three-array slice approach with a single filter pass.
The new version is more idiomatic, avoids allocating two intermediate
arrays, and removes the need for inline comments explaining the logic.
@JZimz
JZimz force-pushed the claude/fervent-einstein-16j58 branch from eca8509 to 91e4398 Compare April 25, 2026 01:35
@JZimz
JZimz merged commit 2e7a3cc into main Apr 25, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant