【发布时间】:2020-09-05 18:48:24
【问题描述】:
我知道 javascript 具有有助于创建这种类型的数据结构的 push / shift / pop 属性。但我知道在巨量数据上使用它们并不是一个好主意,因为它必须遍历整个数组才能执行操作。那么.. 高效代码的示例是什么样的?
这是我的代码,但是当删除元素时使用“dequeue”时,即使它的值为“null”,它仍然存储在内存中,我该如何避免这种情况?
class Queue {
constructor() {
this.items = {},
this.front = 0,
this.end = 0;
}
enqueue(data) {
this.items[this.end] = data;
this.end++;
}
dequeue() {
if (this.front === this.end) {
return null;
}
const data = this.item[this.front];
this.front++;
return data;
}
getSize() {
return this.end - this.front;
}
}
【问题讨论】:
标签: javascript object data-structures queue