Guest User

Untitled

a guest
Oct 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. function Stack(){
  2. var stack = {};
  3. var stackSize = 0;
  4.  
  5. return {
  6. push: function(item){
  7. stack[stackSize] = item;
  8. stackSize++;
  9. },
  10. pop: function(){
  11. if (this.isEmpty()){ return undefined;}
  12.  
  13. stackSize--;
  14.  
  15. var item = stack[stackSize];
  16. delete stack[stackSize];
  17.  
  18. return item;
  19. },
  20. peek: function(){
  21. if (this.isEmpty()){
  22. return undefined;
  23. }
  24.  
  25. return stack[stackSize - 1];
  26. },
  27. stackEmpty: function(){
  28. while (!this.isEmpty()){
  29. this.pop();
  30. }
  31. },
  32. isEmpty: function(){
  33. return stackSize === 0;
  34. },
  35. size: function(){
  36. return stackSize;
  37. },
  38. print: function(){
  39. var result = [];
  40.  
  41. for (var key in stack){
  42. result.unshift(stack[key]);
  43. }
  44.  
  45. return result;
  46. }
  47. }
  48. }
  49.  
  50. const Capability = 50000000;
  51. const TestCase = 10;
  52. const st = new Stack();
  53.  
  54. const start = performance.now();
  55.  
  56. for (let tc = 0; tc < TestCase; tc++) {
  57. for (let i = 0; i < Capability; i++) {
  58. st.push(i);
  59. }
  60.  
  61. for (let i = 0; i < Capability; i++) {
  62. st.pop();
  63. }
  64. }
  65.  
  66. const end = performance.now();
  67. const duration = (end - start) / TestCase;
  68.  
  69. console.log(`Duration: ${duration} ms`);
Add Comment
Please, Sign In to add comment