Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const print = str => document.write(str + '<br>');
- const ITER_COUNT = 10000;
- let t0, t1;
- let stack, queue;
- print("====== STACK ======");
- class ArrayStack {
- constructor() {
- this.arr = [];
- }
- push(val) {
- this.arr.push(val);
- }
- pop() {
- return this.arr.pop();
- }
- }
- stack = new ArrayStack();
- t0 = performance.now();
- for (let i = 0; i < ITER_COUNT; i++) {
- for (let j = i; j >= 0; j--) {
- stack.push(i);
- }
- for (let j = i; j >= 0; j--) {
- stack.pop();
- }
- }
- t1 = performance.now();
- print(`ArrayStack finished in ${t1 - t0}ms`);
- class Node {
- constructor(val) {
- this.val = val;
- this.next = 0;
- }
- }
- class LinkedListStack {
- constructor() {
- this.list = null;
- }
- push(val) {
- const node = new Node(val);
- node.next = this.list;
- this.list = node;
- }
- pop() {
- if (!this.list) return null;
- const val = this.list.val;
- this.list = this.list.next;
- return val;
- }
- }
- stack = new LinkedListStack();
- t0 = performance.now();
- for (let i = 0; i < ITER_COUNT; i++) {
- for (let j = i; j >= 0; j--) {
- stack.push(i);
- }
- for (let j = i; j >= 0; j--) {
- stack.pop();
- }
- }
- t1 = performance.now();
- print(`LinkedListStack finished in ${t1 - t0}ms`);
- print("");
- print("====== QUEUE ======");
- class ArrayQueue {
- constructor() {
- this.arr = [];
- }
- enqueue(val) {
- this.arr.push(val);
- }
- dequeue() {
- return this.arr.shift();
- }
- }
- queue = new ArrayQueue();
- t0 = performance.now();
- for (let i = 0; i < ITER_COUNT; i++) {
- for (let j = i; j >= 0; j--) {
- queue.enqueue(i);
- }
- for (let j = i; j >= 0; j--) {
- queue.dequeue();
- }
- }
- t1 = performance.now();
- print(`ArrayQueue finished in ${t1 - t0}ms`);
- class LinkedListQueue {
- constructor() {
- this.front = null;
- this.rear = null;
- }
- enqueue(val) {
- const node = new Node(val);
- if (this.rear) {
- this.rear.next = node;
- }
- this.rear = node;
- if (!this.front) {
- this.front = this.rear;
- }
- }
- dequeue() {
- if (!this.front) return null;
- const val = this.front.val;
- this.front = this.front.next;
- if (!this.front) {
- this.rear = null;
- }
- return val;
- }
- }
- queue = new LinkedListQueue();
- t0 = performance.now();
- for (let i = 0; i < ITER_COUNT; i++) {
- for (let j = i; j >= 0; j--) {
- queue.enqueue(i);
- }
- for (let j = i; j >= 0; j--) {
- queue.dequeue();
- }
- }
- t1 = performance.now();
- print(`LinkedListQueue finished in ${t1 - t0}ms`);
- class CircularQueue {
- constructor(arrSize) {
- this.arrSize = arrSize;
- this.arr = new Array(this.arrSize);
- this.size = 0;
- this.front = 0;
- this.rear = 0;
- }
- enqueue(val) {
- this.arr[this.rear] = val;
- this.rear = (this.rear + 1) % this.arrSize;
- this.size++;
- }
- dequeue() {
- if (this.front === this.rear) return null;
- const val = this.arr[this.front];
- this.front = (this.front + 1) % this.arrSize;
- this.size--;
- return val;
- }
- }
- queue = new CircularQueue(ITER_COUNT);
- t0 = performance.now();
- for (let i = 0; i < ITER_COUNT; i++) {
- for (let j = i; j >= 0; j--) {
- queue.enqueue(i);
- }
- for (let j = i; j >= 0; j--) {
- queue.dequeue();
- }
- }
- t1 = performance.now();
- print(`CircularQueue with size ${ITER_COUNT} finished in ${t1 - t0}ms`);
- queue = new CircularQueue(ITER_COUNT * 100);
- t0 = performance.now();
- for (let i = 0; i < ITER_COUNT; i++) {
- for (let j = i; j >= 0; j--) {
- queue.enqueue(i);
- }
- for (let j = i; j >= 0; j--) {
- queue.dequeue();
- }
- }
- t1 = performance.now();
- print(`CircularQueue with size ${ITER_COUNT * 100} finished in ${t1 - t0}ms`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement