title: Array with unique values: indexOf vs new Set
body: <p>Assuming we have an array with repeated items like so:</p>
  
  <pre><code>var items = ['a', 'd', 'e', 'b', 'c', 'd', 'e', 'f', 'g', 'd', 'f', 'g', 'd', 'j', 'k', 'l', 'd', 'e', 'c', 'd', 'e', 'f', 'g', 'd','c', 'd', 'e', 'f', 'g', 'd'];
  </code></pre>
  
  <p>What would be the fastest way to have a new array with the values, given that you <strong>must</strong> loop through the items (might make no sense, but that's the scenario).</p>
  
  <p>Option 1:</p>
  
  <pre><code>var list = [];
  items.forEach(function(item) {
      if(list.indexOf(item) == -1)
        list.push(item);
  });
  </code></pre>
  
  <p>Option 2:</p>
  
  <pre><code>var list = [];
  items.forEach(function(item) {
      list.push(item);
  });
  
  list = Array.from(new Set(list));
  </code></pre>
  
  <p>Now I have done some testing with console.time and it shows that the option 2 is 5 times faster that the option 1. But I am not sure how trustable this console.time is. 
  Any insight? Is the <code>indexOf</code> what's making the option 1 slower?</p>
  
  <p>Fiddle: <a href="https://jsfiddle.net/q9opqvsm/" rel="nofollow noreferrer">https://jsfiddle.net/q9opqvsm/</a></p>
  
  <p>Edit: Another question: if option 2 is faster, should I change my code from option 1 => option 2. If not, why?</p>