Data Structures in Practice: Arrays vs. Linked Lists vs. Hash Maps
Different Shapes for Different Jobs
Every data structure is a tradeoff, not a universal best choice. The three you'll reach for constantly - arrays, linked lists, and hash maps (objects/dictionaries) - each win at something specific and lose at something else.
Array: fast lookup by index (O(1)), slow insert/remove in the middle (O(n))
Linked list: fast insert/remove anywhere once you're there (O(1)), slow lookup (O(n))
Hash map: fast lookup/insert/remove by key (O(1) average), no guaranteed orderIn practice, most day-to-day code reaches for arrays and hash maps almost exclusively - true linked lists are rarer outside of specific systems programming (they show up inside databases and language runtimes more than in typical application code). The decision that actually matters most often: array vs. hash map.
A fast tell: if you find yourself writing `array.find(x => x.id === someId)` inside a loop, that's an O(n) search happening repeatedly - almost always a sign you should build a hash map keyed by id once, up front, and look things up in O(1) from then on.
// O(n) every time - searching the array
const user = users.find(u => u.id === targetId);
// O(1) after one O(n) setup - build it once
const usersById = Object.fromEntries(users.map(u => [u.id, u]));
const user2 = usersById[targetId];