Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it worth creating a LinkedList in Java Script

I am currently working on a project that requires me to iterate through a list of values and add a new value in between each value already in the list. This is going to be happening for every iteration so the list will grow exponentially. I decided that implementing the list as a Linked List would be a great idea. Now, JS has no default Linked List data structure, and I have no problem creating one.

But my question is, would it be worth it to create a simple Linked List from scratch, or would it be a better idea to just create an array and use splice() to insert each element? Would it, in fact, be less efficient due to the overhead?

like image 762
Dimi Avatar asked Mar 09 '14 08:03

Dimi


People also ask

Why would you use a linked list in Javascript?

LinkedList is the dynamic data structure, as we can add or remove elements at ease, and it can even grow as needed. Just like arrays, linked lists store elements sequentially, but don't store the elements contiguously like an array. Now, Let's see an example of a Linked List Node: Javascript.

Are linked lists ever useful?

Linked lists are often used because of their efficient insertion and deletion. They can be used to implement stacks, queues, and other abstract data types.

Are linked lists outdated?

Linked lists (doubly linked lists) still have valid uses, but it's generally better to use an array (or vector) until you can justify otherwise.

What is the main disadvantage of a linked list?

Disadvantages of Linked Lists: Searching an element is costly and requires O(n) time complexity. Traversing is more time consuming and reverse traversing is not possible in singly linked lists. Random access is not possible due to dynamic memory allocation.


1 Answers

Use a linked list, in fact most custom implementations done well in user javascript will beat built-in implementations due to spec complexity and decent JITting. For example see https://github.com/petkaantonov/deque

What george said is literally 100% false on every point, unless you take a time machine to 10 years ago.


As for implementation, do not create external linked list that contains values but make the values naturally linked list nodes. You will otherwise use way too much memory.

like image 148
Esailija Avatar answered Sep 19 '22 14:09

Esailija