Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Array, Stack, Queue - what is the motivation behind this specific API design?

In my day-to-day work i mostly use C# and only use javascript occasionally, so please, javascript Gurus don't judge my questions roughly!

  1. Array implements the Stack by providing the push and pop method, but peek is missing, why? (yes it is trivial to implement, but still)
  2. Array implements Queue but the operations are named push-shift orunshift-pop instead of enqueue and dequeue, why name them differently? Is this inspired by Python and Ruby?
  3. Why APIs for Array, Stack and Queue are merged into one object, instead of segregating the interface and having different objects for that? Is it because the implementation is cheap?
  4. Semantically in many languages (C#, C++, Java) an Array is a continuous block in memory and is not re-sizable. On the other hand, the basic collection that allows easy addition of elements is a List (ArrayList or LinkedList or the like). Would it not be better if Array was named a List in javascript?
  5. How is Array implemented under the hood? Where can I find a very detailed description?
like image 331
ironstone13 Avatar asked May 06 '17 20:05

ironstone13


People also ask

What are stacks and queues in JavaScript?

Stacks and Queues are always the most discussed data structures. This is because they both have opposite operations. Stack follows the pattern LIFO — Last In First Out whereas Queues uses FIFO — First In First Out. A common example of a Stack is a pile of plates while that of Queue is a queue at a bus station.

Can a JavaScript array be used as a stack?

JavaScript Array type provides the push() and pop() methods that allow you to use an array as a stack.

What is the most efficient method to remove an element from the queue using JavaScript arrays?

We have used push() method of array to add an element at the end of the queue. This function removes an element from the front of a queue . We have used shift method of an array to remove an element from the queue.

What is stack in JavaScript?

A stack is an ordered collection of items that follow the Last In First Out (LIFO) principle. The addition and removal of items take place at the same end, i.e. at the top. The newest elements are at the top, and the oldest elements are at the bottom.

How do you implement stack and queue in JavaScript?

In Javascript the implementation of stacks and queues is as follows: Stack: A stack is a container of objects that are inserted and removed according to the last-in-first-out (LIFO) principle. Push: Method adds one or more elements to the end of an array and returns the new length of the array.

Why are queue methods in JavaScript so inefficient?

Although this is a simple way to implement queues, it is very inefficient for large queues — because of the methods operate on arrays, the shift and unshift methods move every element in the array each time they are called. Queue.js is a simple and efficient queue implementation for JavaScript whose dequeue function runs in amortized constant time.

Why is an array called a queue in JavaScript?

Because that is what was queued. The regular Array structure in Javascript is a Stack (first in, last out) and can also be used as a Queue (first in, first out) depending on the calls you make.

What are stacks and queue data structures?

In this article, we've explored two linear data structures: stacks and queues. A stack stores data in sequential order and removes the most recently added data; a queue stores data in sequential order but removes the oldest added data. If the implementation of these data structures seems trivial, remind yourself of the purpose of data structures.


1 Answers

  1. JavaScript was invented in 10 days, peek was never added since, mainly because it is easy to implement, maybe one day.

  2. Differently from what you know yes

  3. It is a high level programming language, also check out the Typed arrays

  4. It could have been named otherwise, it is too late now.

  5. It depends on the Engine, mostly like in Python

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

like image 157
Walle Cyril Avatar answered Oct 13 '22 23:10

Walle Cyril