An implementation of a stack using javascript objects
Creates a new stack from provided arguments. any arrays in the arguments are flattened.
deletes all items in the stack (alias for Stack.prototype.empty)
deletes all items in the stack
returns true if stack is empty, otherwise returns false.
returns last item in stack without removing it
pops the last item off the stack.
pushes an item to the stack.
pushes many items to the stack in the order provided. any arrays in the arguments are flattened.
returns the current size of the stack.
swaps the last two items in the stack
these are not meant to be accessed during normal use
the number of items in the stack
all the items in the stack
const Stack = require('stack-js')
let stack = new Stack()
stack.isEmpty() // true
stack.push(2)
stack.isEmpty() // false
stack.push(3)
stack.push(4)
stack.size() // 3
stack.peek() // 4
stack.pop() // 4
stack.size() // 2
stack.pop() // 3
stack.empty()
stack.size() // 0
stack = Stack.from(1,2,[3,4],5)
stack.size() // 5
stack.swap()
stack.pop() // 4
stack.pop() // 5
Simple stack operations should have a time complexity of O(1). Most methods on the Array Prototype have a time complexity of O(n). Arrays take up a block of space because they have to keep their order. Objects however, have direct look-up of properties and do not have to be kept ‘in-order’.