I'm not sure why one would do this over simply using an array... Delete in particular is a somewhat costly execution penalty.
class Queue {
constructor() {
this._items = [];
}
enqueue(item) {
this._items.push(item);
}
dequeue() {
return this._items.shift();
}
peek() {
return this._items[0];
}
get length() {
return this._items.length;
}
}
Then use splice if you're really concerned about it... will be O(n) with an n of 2.
Also, have you actually measured the performance cost of Delete... as I mentioned, you're optimizing by using an expensive call.
"Then use splice if you're really concerned about it... will be O(n) with an n of 2."
enqueue() and dequeue() operations on a queue have to be O(1) time complexity. Using unshift(), splice() is a mistake, they are O(n).
Your proposal is to use an O(n) complexity function splice() instead of delete O(1) - but that's just a mistake.
"Also, have you actually measured the performance cost of Delete... as I mentioned, you're optimizing by using an expensive call."
delete operator is O(1) time complexity. That's all that matters.
If you are concerned about the delete operator performance (which in 99% you shouldn't be), use a Map instead of a plan JavaScript object, then use map.delete(key).
You are correct, I apologize. I will note, that `undefined` assignment is faster than deletion. I wasn't able to get a good/consistent memory profile for comparison. I was mistaken about how Array is implemented from a couple articles that I'd read, the impression was slice would only effect the starting index and the number of items in the array affected, not the entire series.
Depending on how big the queue might get, it might be worth exploring using a linked-list instead of object refs.
https://github.com/tracker1/queue-testing