title: How to sort and search in JavaScript large set
body: <p>The array &quot;scores&quot; tells the total points for each person involved in a contest. So for example:</p>
  <pre><code>User A: 100 points
  User B: 90 points
  User C: 90 points
  User D: 80 points
  User E: 75 points
  User F: 60 points
  </code></pre>
  <p>According to above scores we will have this ranking:</p>
  <pre><code>User A: #1
  User B: #2  
  User C: #2
  User D: #3
  User E: #4
  User F: #5
  </code></pre>
  <p>This ranking method follows the Dense Ranking method.</p>
  <p>Then we have a user named alice. If she gets 55 points, she will rank at position #6 (according to ranking above).
  If she scores 90 points, she will rank at position #2. And so on.</p>
  <p>I actually have an array containing different &quot;sessions&quot; for alice. So having for example:<br />
  [55, 90]</p>
  <p>This means that first time will alice be ranked at position #6. While second time she will be ranked at position #2.</p>
  <p>I coded this, and it works. However, this does not seem to be very efficient. For large datasets, with half million entries in the scores-array, it times out. This is the code:</p>
  <pre><code>const getPosition = (element, scores) =&gt; {
      scores.push(element);
      scores.sort(function (a,b) { return b-a; });
      return scores.indexOf(element)+1;
  }
  
  function climbingLeaderboard(scores, alice) {
      var uniqueSet = new Set(scores);
      scores = [...uniqueSet];
      var positions = [];
      let aliceIndex = 0;
      while(aliceIndex &lt; alice.length){
          positions.push(getPosition(alice[aliceIndex], scores));
          aliceIndex++;
      }
      return positions;
  }
  
  function main() {
      const scores = [100, 90, 90, 80, 75, 60];
      const alice = [50, 65, 77, 90, 102];
      let result = climbingLeaderboard(scores, alice);
      console.log(result.join(&quot;\n&quot;) + &quot;\n&quot;);
  }
  </code></pre>
  <p>I guess the &quot;sort&quot;-function and/or searching for the element in the array with indexOf is the problem. But I could not find a way to make these two operations more efficient.</p>