Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. const print = str => document.write(str + '<br>');
  2.  
  3. const ITER_COUNT = 10000;
  4.  
  5. let t0, t1;
  6. let stack, queue;
  7.  
  8. print("====== STACK ======");
  9.  
  10. class ArrayStack {
  11. constructor() {
  12. this.arr = [];
  13. }
  14.  
  15. push(val) {
  16. this.arr.push(val);
  17. }
  18.  
  19. pop() {
  20. return this.arr.pop();
  21. }
  22. }
  23.  
  24. stack = new ArrayStack();
  25.  
  26. t0 = performance.now();
  27. for (let i = 0; i < ITER_COUNT; i++) {
  28. for (let j = i; j >= 0; j--) {
  29. stack.push(i);
  30. }
  31. for (let j = i; j >= 0; j--) {
  32. stack.pop();
  33. }
  34. }
  35. t1 = performance.now();
  36.  
  37. print(`ArrayStack finished in ${t1 - t0}ms`);
  38.  
  39. class Node {
  40. constructor(val) {
  41. this.val = val;
  42. this.next = 0;
  43. }
  44. }
  45.  
  46. class LinkedListStack {
  47. constructor() {
  48. this.list = null;
  49. }
  50.  
  51. push(val) {
  52. const node = new Node(val);
  53. node.next = this.list;
  54. this.list = node;
  55. }
  56.  
  57. pop() {
  58. if (!this.list) return null;
  59. const val = this.list.val;
  60. this.list = this.list.next;
  61. return val;
  62. }
  63. }
  64.  
  65. stack = new LinkedListStack();
  66.  
  67. t0 = performance.now();
  68. for (let i = 0; i < ITER_COUNT; i++) {
  69. for (let j = i; j >= 0; j--) {
  70. stack.push(i);
  71. }
  72. for (let j = i; j >= 0; j--) {
  73. stack.pop();
  74. }
  75. }
  76. t1 = performance.now();
  77.  
  78. print(`LinkedListStack finished in ${t1 - t0}ms`);
  79.  
  80. print("");
  81.  
  82. print("====== QUEUE ======");
  83.  
  84. class ArrayQueue {
  85. constructor() {
  86. this.arr = [];
  87. }
  88.  
  89. enqueue(val) {
  90. this.arr.push(val);
  91. }
  92.  
  93. dequeue() {
  94. return this.arr.shift();
  95. }
  96. }
  97.  
  98. queue = new ArrayQueue();
  99.  
  100. t0 = performance.now();
  101. for (let i = 0; i < ITER_COUNT; i++) {
  102. for (let j = i; j >= 0; j--) {
  103. queue.enqueue(i);
  104. }
  105. for (let j = i; j >= 0; j--) {
  106. queue.dequeue();
  107. }
  108. }
  109. t1 = performance.now();
  110.  
  111. print(`ArrayQueue finished in ${t1 - t0}ms`);
  112.  
  113. class LinkedListQueue {
  114. constructor() {
  115. this.front = null;
  116. this.rear = null;
  117. }
  118.  
  119. enqueue(val) {
  120. const node = new Node(val);
  121. if (this.rear) {
  122. this.rear.next = node;
  123. }
  124. this.rear = node;
  125. if (!this.front) {
  126. this.front = this.rear;
  127. }
  128. }
  129.  
  130. dequeue() {
  131. if (!this.front) return null;
  132. const val = this.front.val;
  133. this.front = this.front.next;
  134. if (!this.front) {
  135. this.rear = null;
  136. }
  137. return val;
  138. }
  139. }
  140.  
  141. queue = new LinkedListQueue();
  142.  
  143. t0 = performance.now();
  144. for (let i = 0; i < ITER_COUNT; i++) {
  145. for (let j = i; j >= 0; j--) {
  146. queue.enqueue(i);
  147. }
  148. for (let j = i; j >= 0; j--) {
  149. queue.dequeue();
  150. }
  151. }
  152. t1 = performance.now();
  153.  
  154. print(`LinkedListQueue finished in ${t1 - t0}ms`);
  155.  
  156. class CircularQueue {
  157. constructor(arrSize) {
  158. this.arrSize = arrSize;
  159. this.arr = new Array(this.arrSize);
  160. this.size = 0;
  161. this.front = 0;
  162. this.rear = 0;
  163. }
  164.  
  165. enqueue(val) {
  166. this.arr[this.rear] = val;
  167. this.rear = (this.rear + 1) % this.arrSize;
  168. this.size++;
  169. }
  170.  
  171. dequeue() {
  172. if (this.front === this.rear) return null;
  173. const val = this.arr[this.front];
  174. this.front = (this.front + 1) % this.arrSize;
  175. this.size--;
  176. return val;
  177. }
  178. }
  179.  
  180. queue = new CircularQueue(ITER_COUNT);
  181.  
  182. t0 = performance.now();
  183. for (let i = 0; i < ITER_COUNT; i++) {
  184. for (let j = i; j >= 0; j--) {
  185. queue.enqueue(i);
  186. }
  187. for (let j = i; j >= 0; j--) {
  188. queue.dequeue();
  189. }
  190. }
  191. t1 = performance.now();
  192.  
  193. print(`CircularQueue with size ${ITER_COUNT} finished in ${t1 - t0}ms`);
  194.  
  195. queue = new CircularQueue(ITER_COUNT * 100);
  196.  
  197. t0 = performance.now();
  198. for (let i = 0; i < ITER_COUNT; i++) {
  199. for (let j = i; j >= 0; j--) {
  200. queue.enqueue(i);
  201. }
  202. for (let j = i; j >= 0; j--) {
  203. queue.dequeue();
  204. }
  205. }
  206. t1 = performance.now();
  207.  
  208. print(`CircularQueue with size ${ITER_COUNT * 100} finished in ${t1 - t0}ms`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement