no-for-in
for (const key in object).
Why: for...in also walks enumerable keys inherited from the prototype chain, and V8 can only make it fast while the object doesn't change (v8.dev). Object.keys and Object.entries give exactly the object's own keys.
✕ Rejectedno-for-in
ts
export function keys(settings: { readonly theme: string; readonly size: number }): string[] {
const out: string[] = [];
for (const key in settings) out.push(key);
return out;
}✓ Accepted0 diagnostics
ts
export function keys(settings: { readonly theme: string; readonly size: number }): string[] {
const out: string[] = [];
for (const key of Object.keys(settings)) out.push(key);
return out;
}Two forms walk only own keys, and are accepted:
- an object without a prototype that the checker can see being made: a binding (never reassigned) of
Object.create(null)or of an object literal with__proto__: null; - a body that starts with
if (!Object.hasOwn(o, key)) continue;(or is a singleif (Object.hasOwn(o, key)) ...), for objects from elsewhere, such as a library's dictionary.
✓ Accepted0 diagnostics
ts
export function tally(tags: readonly string[]): number {
const counts = { __proto__: null, all: tags.length };
let total = 0;
for (const key in counts) total += key.length;
return total;
}
export function stocked(stock: { readonly [sku: string]: number }): number {
let count = 0;
for (const sku in stock) {
if (!Object.hasOwn(stock, sku)) continue;
count += 1;
}
return count;
}Performance. Which loop is cheapest depends on the object (Node 26.13, one walk over 4 keys, median of 7 runs of a million walks):
| Object | for...in | for...in with Object.hasOwn | Object.keys + lookup | Object.values |
|---|---|---|---|---|
fixed keys ({} filled the same way) | 7 ns, 0 B | 28 ns, 0 B | 31 ns, 81 B | 57 ns, 81 B |
no prototype (Object.create(null), __proto__: null) | 92 ns, 97 B | 115 ns, 97 B | 79 ns, 129 B | 281 ns, 489 B |
V8 keeps an object without a prototype in dictionary mode, and for...in over it collects the keys into a new array on every walk, like Object.keys. On such a dictionary (a renderer's per-frame batches keyed by id), Object.keys with a lookup was the fastest and Object.values three times slower; with 16 keys: 230, 290 and 1 010 ns. for...in saves the array only on objects with a fixed shape.
A library's dictionaries (a renderer's batches, a scene graph's children by id, made with Object.create(null) inside the library): the checker can't see them being made, and a type can't prove that an object has no prototype, so a plain for...in over them is reported. No walk over such an object avoids an allocation: V8 collects its keys into a new array on every for...in too. Write the Object.hasOwn guard (it costs no more memory than for...in itself), or Object.keys with a lookup, the fastest in the table above. Avoid Object.values and Object.entries there: they allocate the most and are several times slower:
✓ Accepted0 diagnostics
ts
export type Batches = { readonly [id: string]: { readonly size: number } };
export function totalSize(batches: Batches): number {
let total = 0;
for (const id in batches) {
if (!Object.hasOwn(batches, id)) continue;
total += batches[id]?.size ?? 0;
}
return total;
}Quick fix: Iterate own keys: for...of Object.keys(...).