Creating a JavaScript object-based Stack class

The easiest way of creating a Stack class is using an array to store its elements. When working with a large set of data (which is very common in real-world projects), we also need to analyze what is the most efficient way of manipulating the data. When working with arrays, most methods have a complexity of time O(n); we will learn more algorithm complexity in the last chapter of this book, Chapter 15, Algorithm Complexity. What this means is that, for most methods, we need to iterate through the array until we find the element we are looking for and, in the worst-case scenario, we will iterate through all the positions of the array, where n is the size of the array. If the array has more elements, ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.