Time Complexity
Time complexity is how an algorithm's runtime grows with input size. TypeScript does not change that growth: types are erased. What it does change is how you type the inputs so the compiler cannot hide a linear scan behind a wrong collection type.
Detailed Explanation
O(1), O(n), O(n log n) are statements about the JavaScript that runs after tsc. A generic find on an array is still a scan. A Map.get is still a hash lookup. Annotating the array as User[] instead of any[] does not add a pass, and it does not remove one.
The type-system angle is picking types that match the complexity you think you have.
If you type a lookup table as User[] and then write users.find(u => u.id === id) at a hot site, the types are honest and the complexity is O(n). If you meant O(1), the honest type is Map<string, User> or Record<string, User>. The type will not run the hash for you, but it will stop you from calling .find on a Map and thinking that is the API. Conversely, Record<string, User> makes users[id] look like a field read; it is O(1) average, and it is also the type that hides "missing key" unless you enable noUncheckedIndexedAccess.
Generic algorithms should mention the collection they actually need. A function typed (items: readonly T[]) => T | undefined is advertising a scan. A function typed (items: ReadonlyMap<K, T>, key: K) => T | undefined is advertising a map lookup. Do not take an array and build a Map inside a function called from a loop — the types will allow it, and you have just turned O(n) into O(n) plus an O(n) build on every call. The signature should take the Map if the caller is expected to reuse it.
Type-level programs have their own cost, and it is not wall-clock time of the user's request. A huge union, a deep conditional, a mapped type over hundreds of keys, makes the compiler slower. That is compile-time complexity. It does not change O(n) of the emit. Keep those two costs in separate buckets. If a type is making tsc stall, simplify the type; do not "optimize" the runtime loop in response.
When you document a helper, put the big-O in the comment and put the collection type in the signature. The comment can lie. The signature is what callers have to satisfy.
For the definition of the notations and the usual list of sorts and searches, see the JavaScript time-complexity page. This page is about not letting a TypeScript type pretend a scan is a lookup.
Code Examples
1. The collection type advertises the cost.
// 1. The collection type advertises the cost.
type User = { id: string; email: string };
function byScan(users: readonly User[], id: string): User | undefined {
return users.find((u) => u.id === id); // O(n)
}
function byMap(users: ReadonlyMap<string, User>, id: string): User | undefined {
return users.get(id); // O(1) average
}2. Do not rebuild the map on every call.
// 2. Do not rebuild the map on every call.
function indexById(users: readonly User[]): Map<string, User> {
const map = new Map<string, User>();
for (const user of users) map.set(user.id, user);
return map;
}
const users: User[] = [{ id: "u1", email: "ada@example.com" }];
const index = indexById(users);
console.log(byMap(index, "u1")?.email);3. A Record lookup looks like a field, still O(1), still maybe missing.
// 3. A Record lookup looks like a field, still O(1), still maybe missing.
const cache: Record<string, User> = { u1: users[0] };
const hit = cache["u1"]; // number of key reads does not depend on n
if (hit) console.log(hit.email);Common Gotchas
A precise generic does not make a loop faster
`findFirst<T>` is still `O(n)`. Measure the JavaScript.
Taking `T[]` and searching by id in a hot path is a complexity bug the types will not flag
change the parameter to `Map<string, T>` if callers can afford to index once.
Compiler slowness is a different budget
split a union or alias a mapped type if `tsc` is the thing that is `O(pain)`.
`Record<string, V>` hides misses
a "fast" lookup that throws on `undefined` is still a production incident. Type the miss.