Skip to content

Instantly share code, notes, and snippets.

@hatashiro
Last active February 19, 2019 03:20
Show Gist options
  • Save hatashiro/54e7558c5a2001db6333d0d428dd1f24 to your computer and use it in GitHub Desktop.
Save hatashiro/54e7558c5a2001db6333d0d428dd1f24 to your computer and use it in GitHub Desktop.
Stack and queue implementations in JavaScript
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`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment