Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Stack(){
- var stack = {};
- var stackSize = 0;
- return {
- push: function(item){
- stack[stackSize] = item;
- stackSize++;
- },
- pop: function(){
- if (this.isEmpty()){ return undefined;}
- stackSize--;
- var item = stack[stackSize];
- delete stack[stackSize];
- return item;
- },
- peek: function(){
- if (this.isEmpty()){
- return undefined;
- }
- return stack[stackSize - 1];
- },
- stackEmpty: function(){
- while (!this.isEmpty()){
- this.pop();
- }
- },
- isEmpty: function(){
- return stackSize === 0;
- },
- size: function(){
- return stackSize;
- },
- print: function(){
- var result = [];
- for (var key in stack){
- result.unshift(stack[key]);
- }
- return result;
- }
- }
- }
- const Capability = 50000000;
- const TestCase = 10;
- const st = new Stack();
- const start = performance.now();
- for (let tc = 0; tc < TestCase; tc++) {
- for (let i = 0; i < Capability; i++) {
- st.push(i);
- }
- for (let i = 0; i < Capability; i++) {
- st.pop();
- }
- }
- const end = performance.now();
- const duration = (end - start) / TestCase;
- console.log(`Duration: ${duration} ms`);
Add Comment
Please, Sign In to add comment