Performance: Measure Before Optimizing
JavaScript performance depends on many factors: algorithmic complexity, allocations, DOM work, network latency, parsing, compilation, garbage collection, and host behavior. Micro-optimizing syntax without measurement often produces complexity without meaningful gains.
Start with profiling and identify the actual bottleneck. Improve algorithms and architecture first, then optimize hot paths supported by measurements.
const users = getUsers();
const usersById = new Map(
users.map(user => [user.id, user])
);
const user = usersById.get(500);Building an index can change repeated lookup from scanning the array each time to efficient Map lookup, which can be significant when many lookups are performed.
Measure before optimizing.
Algorithmic improvements usually matter more than syntax-level micro-optimizations.
Use appropriate data structures for access patterns.
What should usually happen before a performance optimization?