Skip to content

Instantly share code, notes, and snippets.

@completejavascript
Created November 20, 2018 03:31
Show Gist options
  • Save completejavascript/ad28ea92d63fcfdd5301efdd98626126 to your computer and use it in GitHub Desktop.
Save completejavascript/ad28ea92d63fcfdd5301efdd98626126 to your computer and use it in GitHub Desktop.
class AQueue {
constructor(capacity) {
this.data = [];
this.capacity = capacity;
this.size = 0;
}
isFull() {
return this.size === this.capacity;
}
isEmpty() {
return this.size === 0;
}
enqueue(item) {
if (this.isFull()) return false;
this.data.push(item);
return true;
}
dequeue() {
if (this.isEmpty()) return undefined;
return this.data.shift();
}
front() {
if (this.isEmpty()) return undefined;
return this.data[0];
}
rear() {
if (this.isEmpty()) return undefined;
return this.data[this.size - 1];
}
clear() {
this.data.length = 0;
this.size = 0;
}
}
// Test
const Capacity = 5000000;
const TestCase = 10;
const queue = new AQueue(Capacity);
const start = performance.now();
for (let tc = 0; tc < TestCase; tc++) {
for (let i = 0; i < Capacity; i++) {
queue.enqueue(i);
}
for (let i = 0; i < Capacity; i++) {
queue.dequeue();
}
}
const end = performance.now();
const duration = (end - start) / TestCase;
console.log(`Duration: ${duration} ms`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment