prefer-set-has

Enforces Set.has() over Array.includes() for constant membership checks in TypeScript.

615|491|Updated Jan 4, 2022
One-click install
npx skills add https://github.com/LedgerHQ/ledger-live --skill prefer-set-has
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: prefer-set-has
Source: https://github.com/LedgerHQ/ledger-live/tree/main/.agents/skills/prefer-set-has
Command: npx skills add https://github.com/LedgerHQ/ledger-live --skill prefer-set-has

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Constant allowlists and blocklists implemented as arrays with .includes() run in O(n) time and obscure the intent of membership testing, leading to slower and less readable TypeScript code.

Core Features & Use Cases

  • Membership Check Guidance: Directs reviewers and authors to use a Set with .has() for constant allowlists, blocklists, and enum-like value sets.
  • Code Review Support: Provides clear bad-versus-good TypeScript examples to reference during pull request reviews.
  • Scope Boundaries: Clarifies when .includes() remains acceptable, such as dynamic or short-lived arrays like function parameters and user input.
  • Use Case: While reviewing a TypeScript pull request, you notice a constant blocklist checked with .includes(); apply this guidance to convert it to a Set with .has() for O(1) lookups.

Quick Start

Review this TypeScript file and convert any constant allowlist or blocklist using Array.includes() into a Set with .has().

Frequently Asked Questions about prefer-set-has

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I replace Array.includes() with Set.has() in TypeScript?

Declare the constant list as a Set, for example const ALLOWED = new Set(["a", "b", "c"]), then test membership with ALLOWED.has(value). This changes lookups from O(n) to O(1) and makes membership intent explicit.

When should I use Set.has() instead of Array.includes()?

Use Set.has() when checking membership in a constant list of known values such as allowlists, blocklists, or enum-like sets. It communicates membership-test intent and avoids accidental mutation of a backing array.

Is Array.includes() ever acceptable for membership checks?

Yes, .includes() is fine for dynamic or short-lived arrays such as function parameters or user input, where constructing a Set would add unnecessary noise without meaningful performance gain.

Why is Set.has() faster than Array.includes()?

Set.has() runs in O(1) constant time because Sets use hash-based lookup, while Array.includes() scans elements linearly in O(n) time. The difference matters most for large or frequently queried constant lists.

Does this guidance apply outside TypeScript code reviews?

The guidance targets TypeScript files specifically, both when writing new membership checks and when reviewing existing ones. The underlying O(1) versus O(n) principle generalizes, but the examples and scope are TypeScript-focused.