Skip to content

Commit 799e6d4

Browse files
committed
Optimize dequeue
1 parent ed5e799 commit 799e6d4

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

JavaScript/7-current.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ class UnrolledQueue {
8181

8282
dequeue() {
8383
if (this.#length === 0) return null;
84-
const item = this.#current.dequeue();
84+
const current = this.#current;
85+
const item = current.dequeue();
8586
this.#length--;
86-
if (this.#current.length === 0 && this.#current !== this.#head) {
87-
const node = this.#current;
88-
this.#current = this.#current.next;
89-
node.reset();
90-
this.#tail.next = node;
91-
this.#tail = node;
87+
if (current.length === 0) {
88+
current.reset();
89+
if (current !== this.#head) {
90+
this.#current = current.next;
91+
this.#tail.next = current;
92+
this.#tail = current;
93+
}
9294
}
9395
return item;
9496
}

0 commit comments

Comments
 (0)