Shift vs Queue vs Slice (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
class Queue {
	enqueue(item) {
		const node = {data: item};
		if (this.tail) {
			this.tail.next = node;
			node.prev = this.tail;
			this.tail = node;
		} else {
			this.head = this.tail = node;
		}
	}
	dequeue() {
		if (!this.head) {
			return null;
		}
		const popped = this.head;
		if (this.head === this.tail) {
			this.head = this.tail = null;
		} else {
			this.head = this.head.next;
		}
		return popped.data;
	}
}
</script>

Setup

var COUNT = 1_000;
var queue = new Queue();
var list = [];

Teardown

delete COUNT;
delete queue;
delete list;

Test runner

Ready to run.

Testing in
TestOps/sec
Shift
for (let i = 0; i < COUNT; i++) {
	list.push(i);
}
for (let i = 0; i < COUNT; i++) {
	const popped = list.shift();
}
ready
Queue
for (let i = 0; i < COUNT; i++) {
	queue.enqueue(i);
}
for (let i = 0; i < COUNT; i++) {
	const popped = queue.dequeue();
}
ready
Slice
for (let i = 0; i < COUNT; i++) {
	list.push(i);
}
for (let i = 0; i < COUNT; i++) {
	const popped = list[0];
	list = list.slice(1);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.