6

I'm learning Algorithms and DS. How can I use queue in JavaScript?

I get that you can do something like this.

var stack = [];
stack.push(2);       // stack is now [2]
stack.push(5);       // stack is now [2, 5]
var i = stack.pop(); // stack is now [2]
alert(i);            // displays 5

var queue = [];
queue.push(2);         // queue is now [2]
queue.push(5);         // queue is now [2, 5]
var i = queue.shift(); // queue is now [5]
alert(i);              // displays 2

But wouldn't shift() would shift everything Hence, time complexity is O(N) instead of Dequeue in Java which is O(1)

Why doesn't JavaScript has the concept of Queue like Stack(array) natively?

I was just curious. Please enlighten me.

(I asked myself this question but couldn't find a valid reason why ES8 or ES9 would have queues inbuilt with Dequeue O(1) and enqueue O(1) without having to implement one myself)

PS: Sorry for asking silly question but this has been itching my brain!

8
  • Just as you mentioned, Array is designed as a very flexible object that you can mimic array, stack and queue operations by calling different methods. I don't think its time complexity is O(N) for such operation. Commented Aug 16, 2017 at 3:18
  • Any shift or unshift () operation will re-create the array. Hence it will be O(N) if i'm not mistaken. Commented Aug 16, 2017 at 3:20
  • How do you know JS implementations haven't optimised .shift() to not rebuild the whole array? Commented Aug 16, 2017 at 3:23
  • https://stackoverflow.com/questions/22614237/javascript-runtime-complexity-of-array-functions Shift is O(1) Only in special cases Commented Aug 16, 2017 at 3:26
  • 1
    @djna yup, that's called Murphy's Law, :-) Commented Jun 17, 2021 at 9:46

1 Answer 1

7

You could implement it from scratch in vanilla JS. As to why it's not native, probably because that efficiency gain is hardly ever necessary and arrays/objects are flexible enough to use.

function Queue() {
    this._oldestIndex = 1;
    this._newestIndex = 1;
    this._storage = {};
}

Queue.prototype.size = function() {
    return this._newestIndex - this._oldestIndex;
};

Queue.prototype.enqueue = function(data) {
    this._storage[this._newestIndex] = data;
    this._newestIndex++;
};

Queue.prototype.dequeue = function() {
    var oldestIndex = this._oldestIndex,
        newestIndex = this._newestIndex,
        deletedData;

    if (oldestIndex !== newestIndex) {
        deletedData = this._storage[oldestIndex];
        delete this._storage[oldestIndex];
        this._oldestIndex++;

        return deletedData;
    }
};
Sign up to request clarification or add additional context in comments.

4 Comments

I have a question. I'm the OP, got an idea. Why can't we SWAP(arr[0], arr[len-1]) and then do a pop() and then do the swap again so that the original order is preserved?
You can't swap js has no reference concept all is a value.
@Et7f3XIV That's not entirely accurate. In Javascript, all objects are passed by reference to the object location. While there is no way to read the address of the object, whenever you assign the object to a new value, you are truly only assigning the reference of the object. If this is an array of objects, then a standard swap operation could be easily used.
Yes you are right we can swap but not with the prototype given in the comment. We would need to add a array parameter and give index (or use indexOf but this would be brittle)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.