var line = new Queue("Bono", "Edge", "Larry");
line.enqueue("Adam");
line.dequeue() // Bono
The only thing I definitely don’t like about this naive implementation is that removing an item from the front of an array is a performance issue. Or is it? There is a theoretically faster version below that is actually slower in practice, so don’t use it! My guess is the various JS engines optimize Array behavior (see here)
Usage of this should be the same as the earlier. In my cursory testing, As I mention below, it is slower than the shift approach for some bizarre reason.
The biggest problem with this version is that cloning will wipe any null/undefined entries in the queue.
var q = new Queue(null)
q.contains(null) //-> true
q.clone().contains(null) //-> false
You’re also growing the items array indefinitely as you add new items, even when removing old ones. This would be less of a problem if you deleted instead of set to null, because then you’d at least have a sparse array not physically using every slot up to its length. But eventually (though unlikely) you’ll also hit a start which will break the queue. That value would be… let me see here… 9007199254740992 I think? No, wait, it would really by the max range of length which is 4294967295. After that, you’re adding into indices length no longer reports for which can mess up your peek().
You’ll have to publish your tests along with your code next time . Has there been much on kirupa.com about testing code?
Publishing tests is a good idea. I haven’t written about that at all, so that’s something to look into. I did create a perf test a few seconds ago: https://jsperf.com/queuekirupa
The “faster” queue implementation is actually slower. In the test, the length of the items is a fixed amount. Only the dequeue runs. It may be that JS engines optimize shift already as seen here: https://bugs.chromium.org/p/v8/issues/detail?id=3059, so any extra code I write will be slower than the C++ implementation under the covers.
Regarding your feedback about null getting obliterated in this approach, that’s true! Let me tweak the code a bit and also use this.start instead of filtering to get the new array during cloning.