title: JS: Is using a dictionary faster than looping through an array?
body: <p>I'm building some program in Nodejs, which will need to keep track <strong>in memory</strong> of  a large number of users. Also, i will have a function that filters a user by id. The code would look something like this:</p>
  
  <pre><code>const users = [
      {
          id: 1,
          name: 'John',
          friends: [3, 6, 8]
      },
      {
          id: 2,
          name: 'Mark',
          friends: [567, 23]
      }
  ]
  
  function getUserById(userId) {
      const user = users.filter(user =&gt; user.id === userId);
      return user[0];
  }
  </code></pre>
  
  <p>The question is, whether this version is generally faster(each key is user id):</p>
  
  <pre><code>const users = {
      1: {
          id: 1,
          name: 'John',
          friends: [3, 6, 8]
      },
      2: {
          id: 2,
          name: 'Mark',
          friends: [567, 23]
      }
  }
  
  function getUserById(userId) {
     return users[userId];
  }
  </code></pre>
  
  <p>My intuition says that the dictionary is faster. What are the facts?</p>