title: Fastest way to compare each item in array with rest of array?
body: <p>I have an array of items, and for each of the item in the array, I need to do some check against the rest of the items in the same array.</p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>const myArray = [ ...some stuff ];
  
  let currentItem;
  let nextItem;
  for (let i = 0; i &lt; myArray.length; i++) {
    currentItem = myArray[i];
    for (let j = i + 1; j &lt; myArray.length; j++) {
      nextItem = myArray[j];
      doSomeComparision(currentItem, nextItem);
    }
  }
  </code></pre>
  
  <p>While this works, I need to find a more efficient algorithm because it slows down significantly if the array is very big.</p>
  
  <p>Can someone provide some advice on how to make this algorithm better?</p>
  
  <h3>Edit 1</h3>
  
  <p>I apologize.</p>
  
  <p>I should have provided more context around what I am trying to do here.
  I am using the loop above with a <code>HalfEdge</code> data structure, a.k.a. DCEL.</p>
  
  <p>Basically, a HalfEdge is an object with 3 properties:</p>
  
  <pre><code>class HalfEdge = {
    head: // some (x,y,z) coords
    tail: // some (x,y,z) coords
    twin: // reference to another HalfEdge
  }
  </code></pre>
  
  <p>A <code>twin</code> of a given <code>HalfEdge</code> is defined like so:</p>
  
  <pre><code>/**
    * if two Half-Edges are twins:
    * Edge A   TAIL ----&gt; HEAD
    *           =          =
    * Edge B   HEAD &lt;---- TAIL
    */
  </code></pre>
  
  <p>My array contains many <code>HalfEdges</code>, and for each <code>HalfEdge</code> in the array, I want to find its twin (i.e., one that satisfies the condition above).</p>
  
  <p>Basically, I am comparing two 3D vectors (one from <code>currentItem</code>, the other from <code>nextItem</code>).</p>
  
  <h3>Edit 2</h3>
  
  <p>Fixed typo in code example (i.e., from <code>let j = 0</code> to <code>let j = i + 1</code>)</p>