title: Linked list vs Array in Javascript
body: <p>So I was playing around with the linked list in JS and came up with the following question:</p>
  
  <p>Lets say, that we have an array and a linked list both with 5000 elements. We want to insert new element at index 10. The array way is pretty simple. We insert the new element at the given index and move the rest of the elements one index forward. So I tried doing this with linked list and end it up with the following:</p>
  
  <p>Getting the implementation of linked list from  <a href="https://github.com/nzakas/computer-science-in-javascript/blob/master/data-structures/linked-list/linked-list.js" rel="noreferrer">Nicholas Zakas</a> and adding additional method addOnPosition(data,index). At the end here is the code:</p>
  
  <pre><code>function LinkedList() {
  this._head = null;
  this._length = 0;
  }
  
  LinkedList.prototype = {
  
  constructor: LinkedList,
  
  add: function(data) {
  
      var node = {
              data: data,
              next: null
          },
          current;
  
      if (this._head === null) {
          this._head = node;
      }
      else {
          current = this._head;
          while (current.next) {
              current = current.next;
          }
          current.next = node;
      }
      this._length++;
  },
  
  remove: function(index) {
  
      if (index &gt; -1 &amp;&amp; index &lt; this._length) {
  
          var current = this._head,
              previous,
              i = 0;
  
  
          if (index === 0) {
              this._head = current.next;
          }
          else {
              while (i++ &lt; index) {
                  previous = current;
                  current = current.next;
              }
              previous.next = current.next;
          }
  
          this._length--;
          return current.data;
      }
      else {
          return null;
      }
  },
  
  item: function(index) {
  
      var current = this._head,
          i = 0;
  
      if (index &gt; - 1 &amp;&amp; index &lt; this._length) {
  
          while (i++ &lt; index) {
              current = current.next;
          }
          return current.data;
  
      }
      else {
          return null;
      }
  },
  
  addOnPosition: function(data,index) {
  
      if (index &gt; -1 &amp;&amp; index &lt;= this._length) {
          var node = {
                  data: data,
                  next: null
              },
              current = this._head,
              i = 0,
              temp,
              previous;
  
          if (this._head === null) {
              this._head = node;
          }
          else {
              if (index === 0) {
                  this._head = node;
                  node.next = current;
              }
              else {
                  while (i++ &lt; index) {
                      previous = current;
                      current = current.next;
                  }
                  previous.next = node;
                  node.next = current;
              }
          }
          this._length++;
      }
      else {
          return null;
      }
  },
  
  toArray: function() {
      var result = [],
          current = this._head;
  
      while (current) {
          result.push(current.data);
          current = current.next;
      }
      return result;
  },
  toString: function() {
      return this.toArray().toString();
  }
  }
  </code></pre>
  
  <p>At the end, my question is: Is this method faster than doing all this with array and if it is, what is the complexity for both?
  And probably the more important, did I missed something with the adOnPosition method implementation?</p>