JavaScript Queue Native

Viewed 3008

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!

1 Answers
Related