Rules Hub

Coding Rules Library

← Back to all rules
fullstack ruleStack: typescript
typescriptclean-codedata-structuresperformance

Prefer Record over Map for simple indexing

Use plain objects (Record) instead of Maps for simple ID-based lookups unless specific Map features are required.

PR: Feat/FCK-2384 - Upgrade MyFa Watchlist functionality #4232Created: Dec 7, 2025

Bad example

Old codets
1const detailsById = new Map<number, TickerDetail>();
2
3tickerDetails.forEach((detail) => {
4 detailsById.set(detail.id, detail);
5});
6
7const item = detailsById.get(someId);

Explanation (EN)

Using a `Map` for simple string or number keys adds unnecessary complexity (`.get`, `.set`) and isn't natively serializable to JSON. It is overkill for simple lookups.

Objašnjenje (HR)

Korištenje `Map` strukture za jednostavne tekstualne ili brojčane ključeve dodaje nepotrebnu složenost (`.get`, `.set`) i ne može se izravno serijalizirati u JSON. To je pretjerano za jednostavna pretraživanja.

Good example

New codets
1const detailsById: Record<number, TickerDetail> = {};
2
3tickerDetails.forEach((detail) => {
4 detailsById[detail.id] = detail;
5});
6
7// Or using reduce:
8// const detailsById = tickerDetails.reduce((acc, detail) => {
9// acc[detail.id] = detail;
10// return acc;
11// }, {} as Record<number, TickerDetail>);
12
13const item = detailsById[someId];

Explanation (EN)

A plain object (`Record`) is idiomatic for key-value pairs where keys are strings or numbers. It is serializable, easier to debug (shows as JSON), and uses standard property access.

Objašnjenje (HR)

Običan objekt (`Record`) je standardan način za parove ključ-vrijednost gdje su ključevi stringovi ili brojevi. Može se serijalizirati, lakši je za debugiranje (prikazuje se kao JSON) i koristi standardni pristup svojstvima.

Notes (EN)

Use `Map` only when keys are objects, frequent additions/removals affect performance significantly, or key insertion order matters.

Bilješke (HR)

Koristite `Map` samo kada su ključevi objekti, kada česta dodavanja/uklanjanja značajno utječu na performanse ili kada je redoslijed umetanja ključeva bitan.