Skip to content

Commit edc9484

Browse files
committed
Optimize implementations
1 parent 2e57b98 commit edc9484

File tree

3 files changed

+37
-62
lines changed

3 files changed

+37
-62
lines changed

JavaScript/1-queue.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ class Queue {
77
return this.#buffer.length;
88
}
99

10-
isEmpty() {
11-
return this.#buffer.length === 0;
12-
}
13-
1410
enqueue(item) {
1511
this.#buffer.push(item);
1612
}
@@ -28,7 +24,7 @@ for (let id = 0; id < 5; id++) {
2824
mq.enqueue({ id });
2925
}
3026

31-
while (!mq.isEmpty()) {
27+
while (mq.length) {
3228
const task = mq.dequeue();
3329
console.log(`Processing ${task.id}`);
3430
}
@@ -37,7 +33,7 @@ for (let id = 100; id < 105; id++) {
3733
mq.enqueue({ id });
3834
}
3935

40-
while (!mq.isEmpty()) {
36+
while (mq.length) {
4137
const task = mq.dequeue();
4238
console.log(`Processing ${task.id}`);
4339
}

JavaScript/2-list.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,29 @@ class ListNode {
99

1010
class Queue {
1111
#length = 0;
12-
13-
constructor() {
14-
this.head = null;
15-
this.tail = null;
16-
}
12+
#head = null;
13+
#tail = null;
1714

1815
get length() {
1916
return this.#length;
2017
}
2118

22-
isEmpty() {
23-
return this.#length === 0;
24-
}
25-
2619
enqueue(item) {
2720
const node = new ListNode(item);
28-
if (this.tail) {
29-
this.tail.next = node;
21+
if (this.#tail) {
22+
this.#tail.next = node;
3023
} else {
31-
this.head = node;
24+
this.#head = node;
3225
}
33-
this.tail = node;
26+
this.#tail = node;
3427
this.#length++;
3528
}
3629

3730
dequeue() {
38-
if (this.isEmpty()) return null;
39-
const item = this.head.value;
40-
this.head = this.head.next;
41-
if (!this.head) this.tail = null;
31+
if (this.#length === 0) return null;
32+
const item = this.#head.value;
33+
this.#head = this.#head.next;
34+
if (!this.#head) this.#tail = null;
4235
this.#length--;
4336
return item;
4437
}
@@ -52,7 +45,7 @@ for (let id = 0; id < 5; id++) {
5245
mq.enqueue({ id });
5346
}
5447

55-
while (!mq.isEmpty()) {
48+
while (mq.length) {
5649
const task = mq.dequeue();
5750
console.log(`Processing ${task.id}`);
5851
}
@@ -61,7 +54,7 @@ for (let id = 100; id < 105; id++) {
6154
mq.enqueue({ id });
6255
}
6356

64-
while (!mq.isEmpty()) {
57+
while (mq.length) {
6558
const task = mq.dequeue();
6659
console.log(`Processing ${task.id}`);
6760
}

JavaScript/4-unrolled.js

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,64 @@
11
'use strict';
22

33
class QueueNode {
4-
#length = 0;
5-
64
constructor({ size }) {
5+
this.length = 0;
76
this.size = size;
8-
this.buffer = new Array(size);
97
this.readIndex = 0;
108
this.writeIndex = 0;
9+
this.buffer = new Array(size);
1110
this.next = null;
1211
}
1312

14-
get length() {
15-
return this.#length;
16-
}
17-
18-
isEmpty() {
19-
return this.#length === 0;
20-
}
21-
22-
isFull() {
23-
return this.#length === this.size;
24-
}
25-
2613
enqueue(item) {
27-
if (this.isFull()) return false;
14+
if (this.length === this.size) return false;
2815
this.buffer[this.writeIndex++] = item;
29-
this.#length++;
16+
this.length++;
3017
return true;
3118
}
3219

3320
dequeue() {
34-
if (this.isEmpty()) return null;
21+
if (this.length === 0) return null;
3522
const item = this.buffer[this.readIndex++];
36-
this.#length--;
23+
this.length--;
3724
return item;
3825
}
3926
}
4027

4128
class UnrolledQueue {
4229
#length = 0;
30+
#nodeSize = 2048;
31+
#head = null;
32+
#tail = null;
4333

4434
constructor(options = {}) {
45-
const { nodeSize = 2048 } = options;
35+
const { nodeSize } = options;
36+
if (nodeSize) this.#nodeSize = nodeSize;
4637
const node = new QueueNode({ size: nodeSize });
47-
this.head = node;
48-
this.tail = node;
49-
this.nodeSize = nodeSize;
38+
this.#head = node;
39+
this.#tail = node;
5040
}
5141

5242
get length() {
5343
return this.#length;
5444
}
5545

56-
isEmpty() {
57-
return this.#length === 0;
58-
}
59-
6046
enqueue(item) {
61-
if (!this.head.enqueue(item)) {
62-
const node = new QueueNode({ size: this.nodeSize });
63-
this.head.next = node;
64-
this.head = node;
65-
this.head.enqueue(item);
47+
if (!this.#head.enqueue(item)) {
48+
const node = new QueueNode({ size: this.#nodeSize });
49+
this.#head.next = node;
50+
this.#head = node;
51+
this.#head.enqueue(item);
6652
}
6753
this.#length++;
6854
}
6955

7056
dequeue() {
71-
const item = this.tail.dequeue();
57+
const item = this.#tail.dequeue();
7258
if (item !== null) {
7359
this.#length--;
74-
if (this.tail.isEmpty() && this.tail.next) {
75-
this.tail = this.tail.next;
60+
if (this.#tail.length === 0 && this.#tail.next) {
61+
this.#tail = this.#tail.next;
7662
}
7763
}
7864
return item;
@@ -87,7 +73,7 @@ for (let id = 0; id < 5; id++) {
8773
mq.enqueue({ id });
8874
}
8975

90-
while (!mq.isEmpty()) {
76+
while (mq.length) {
9177
const task = mq.dequeue();
9278
console.log(`Processing ${task.id}`);
9379
}
@@ -96,7 +82,7 @@ for (let id = 100; id < 105; id++) {
9682
mq.enqueue({ id });
9783
}
9884

99-
while (!mq.isEmpty()) {
85+
while (mq.length) {
10086
const task = mq.dequeue();
10187
console.log(`Processing ${task.id}`);
10288
}

0 commit comments

Comments
 (0)