Every major website and app, from Spotify to Google Maps, depends on core data structures to stay fast and organized. These structures determine how data is stored, accessed, and updated; Without them, tasks like streaming a video, showing a social feed, or finding a route would be painfully inefficient.
In JavaScript (and most programming languages), understanding data structures isn’t just theory; it’s a direct window into how the web actually works. Below is an overview of eight key data structures and how real websites use them to stay quick, dynamic, and scalable.
Stack (LIFO – Last In, First Out)
Before anything else, stacks are among the simplest yet most vital data structures. The concept is straightforward: the last item added is the first one removed. Think of it like a stack of browser tabs or plates on a table.
Real-World Use
- Browser history: Chrome and Firefox use stacks to move backward and forward through pages.
- Undo/redo systems: Applications like VS Code, Photoshop, and Word store recent actions as a stack of states.
- Function calls: JavaScript engines rely on a call stack to keep track of running functions.
Why It Works
Stacks are perfect when actions need to be reversed in order. Each action can be popped off one by one in reverse sequence, making them ideal for history tracking and undo logic.
const history = [];
history.push("google.com");
history.push("github.com");
history.pop(); // returns "github.com"
Queue (FIFO – First In, First Out)
Queues are essentially lines, the first element added being the first to leave. This order makes them ideal for handling processes that need to run in sequence.
Real-World Use
- YouTube & Netflix: Video frames buffer using a queue so playback stays smooth.
- Slack & Discord: Messages are delivered in order through message queues.
- Node.js: The event loop uses queues to manage asynchronous tasks.
Why It Works
Queues guarantee fairness and consistency, ensuring that the first task to arrive is processed first. That’s critical for streaming, messaging, and scheduling tasks.
const queue = ["Task1", "Task2"];
queue.push("Task3");
queue.shift(); // returns "Task1"
Linked List
A linked list connects elements using pointers, each element knowing where the next one is. Unlike arrays, linked lists don’t need continuous memory, so adding or removing items is much more efficient.
Real-World Use
- Spotify & Apple Music: Songs in a playlist are linked, allowing easy navigation between tracks.
- Twitter & Instagram: Infinite scroll feeds often use linked structures to load new posts dynamically.
- Web browsers: Tabs or navigation elements can be linked for quick switching.
Why It Works
Linked lists are flexible and dynamic. They’re great for systems that constantly change — like playlists or social feeds — since they can expand or contract without costly rearrangements.
const song1 = { title: "Track 1", next: null };
const song2 = { title: "Track 2", next: null };
song1.next = song2;
Tree
Trees represent hierarchical data, where one element branches into multiple children. They’re everywhere in modern computing, from your file explorer to the DOM in your browser.
Real-World Use
- Facebook & Reddit: Comment threads are built as trees of replies and subreplies.
- Chrome & Firefox: The DOM (Document Object Model) uses a tree structure to represent page elements.
- Operating systems: Folder and file systems are organized as trees.
Why It Works
Trees naturally express parent–child relationships. They allow data to be searched, inserted, and displayed in nested ways, perfect for organizing everything from posts to webpage elements.
const comment = {
text: "Main comment",
replies: [
{ text: "Reply 1", replies: [] },
{ text: "Reply 2", replies: [] }
]
};
Graph
Graphs take things a step further by modeling connections between entities: not just parent and child, but complex relationships.
Real-World Use
- Facebook & LinkedIn: Graphs represent user connections — who’s friends with whom.
- Google Maps & Uber: Locations and routes form interconnected nodes and paths.
- Recommendation systems: Graphs help suggest new friends, products, or routes based on shared links.
Why It Works
Graphs shine wherever relationships matter. They model social networks, geographic routes, and recommendation logic far more effectively than other structures.
const graph = {
Alice: ["Bob", "Charlie"],
Bob: ["Alice", "Diana"]
};
Set
Sets store unique values — meaning no duplicates. They’re simple but powerful when you need to maintain uniqueness automatically.
Real-World Use
- Twitter & Instagram: Each like, hashtag, or follower is unique — sets enforce that.
- Google Search: Duplicate results are filtered out using set-like logic.
- Reddit: Unique user IDs or upvotes are tracked with sets.
Why It Works
Sets prevent repetition without extra checks. They’re lightweight and efficient for ensuring uniqueness, which is crucial for social data and search filtering.
const likes = new Set(["@joe", "@mary", "@joe"]);
console.log(likes.size); // 2
Hash Table (Hash Map)
Hash tables are one of the most powerful ways to store and retrieve data efficiently. They use a hash function to map keys to specific memory locations, where their corresponding values are stored. Unlike arrays or lists, hash tables don’t care about order: they care about instant access.
In JavaScript, objects and Map() structures both behave like hash tables, but a true hash table can store explicit key–value pairs (tuples) in an array, using hashing to decide where each pair goes.
Real-World Use
YouTube: Maps video IDs to metadata like titles or view counts.
Amazon: Associates product IDs with prices, stock, and descriptions.
Twitter: Maps usernames to user data and post histories.
Why It Works
Hash tables give near-instant access to data using a key rather than a search. By hashing the key, the table knows exactly where to find or insert the value. This design avoids scanning large datasets and makes lookups, insertions, and deletions O(1), on average, ideal for databases, caches, and authentication systems.
const hashTable = {
0: { "user999": "Carl" },
1: { "user123": "Alice", "user321": "Eve" },
2: { "user456": "Bob" },
3: { "user789": "Dana" },
4: {}
};
function hash(key) {
let hashValue = 0;
for (let char of key) {
hashValue = (hashValue + char.charCodeAt(0)) % 5;
}
return hashValue;
}
const key = "user123";
const index = hash(key);
const result = hashTable[index][key];
console.log(result);
Under the Hood
Each key–value pair is stored as a tuple ([key, value]) inside a bucket. The hash function converts the key into an index; If two keys share the same index (a collision), they’re simply added to the same bucket.
This structure mirrors how JavaScript’s Object and Map work behind the scenes, just at a lower, more explicit level, balancing simplicity, speed, and scalability.
Binary Search Tree (BST)
A Binary Search Tree is an ordered structure that allows data to be stored and retrieved efficiently. Each node has up to two children: smaller values on the left, larger on the right.
Real-World Use
- Google Search: Quickly finds relevant terms for autocomplete suggestions.
- Spotify: Sorts and retrieves songs alphabetically or by genre.
- Amazon: Sorts products by price, rating, or availability.
Why It Works
BSTs allow quick searching, sorting, and range queries. When balanced, they make operations nearly instantaneous compared to scanning every element linearly.
class BSTNode {
constructor(value) {
this.value = value; // The data stored in this node
this.left = null; // Points to smaller values
this.right = null; // Points to larger values
}
}
This forms the building block of a binary search tree. When you connect multiple nodes using their left and right pointers, you get a structure that can be searched, expanded, and traversed efficiently.
Conclusion
While most users never think about them, data structures power every click, scroll, and search on the internet. Stacks handle browser navigation, queues keep streaming smooth, graphs map our social lives, and hash tables make search instant.
Just like ES6 improved how we write JavaScript, data structures improve how our programs think. Together, they form the silent architecture behind every modern app, shaping how efficiently data flows across the web.
Top comments (0)