Skip to content

Commit 7357fb6

Browse files
committed
ref: use array instead of map for cache
1 parent 232ac4f commit 7357fb6

2 files changed

Lines changed: 6 additions & 16 deletions

File tree

src/index.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,32 @@
11
export class Queue<T extends defined> {
2-
private readonly cache: Map<number, T>; // cause roblox-ts doesnt let u set specific array elements
32
private first = 0;
43
private last: number;
54

6-
public constructor(cache: T[] = []) {
7-
this.cache = cache as never;
5+
public constructor(private readonly cache: T[] = []) {
86
this.last = cache.size();
97
}
108

119
public enqueue(value: T): void {
12-
const last = this.last++;
13-
this.cache.set(last + 1, value);
10+
this.cache[this.last++] = value;
1411
}
1512

1613
public dequeue(): T | undefined {
1714
if (this.isEmpty()) return;
1815

1916
const first = this.first++;
20-
const value = this.cache.get(first + 1);
21-
this.cache.delete(first);
17+
const value = this.cache[first];
18+
this.cache[first] = undefined!;
2219

2320
return value;
2421
}
2522

2623
public toArray(): T[] {
27-
const result: T[] = [];
28-
for (let i = this.first; i < this.last; i++) {
29-
const value = this.cache.get(i + 1);
30-
if (value === undefined) continue;
31-
result.push(value);
32-
}
33-
34-
return result;
24+
return this.cache.filterUndefined();
3525
}
3626

3727
public at(index: number): T | undefined {
3828
if (index < 0 || index >= this.size()) return;
39-
return this.cache.get(this.first + index + 1);
29+
return this.cache[this.first + index];
4030
}
4131

4232
public isEmpty(): boolean {

tests/test-environment.rbxl

-104 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)