Do I need to worry about Concurrent Modification in Node.js?
<p>I'm writing a small chat application with Node.js and Socket.io.  I have an object, <code>connections</code>, that keeps references to all of the live connections, e.g. (pseudocode):</p>

<pre><code>var connections = {};
...
io.on("connection", function(socket)){
  connections[socket.id] = socket;       // (1)

  socket.on("disconnect", function(){
    delete connections[socket.id];       // (2)
  });
}
</code></pre>

<p>Now in another part of the code I iterate over the connections object, e.g.:</p>

<pre><code>for (var i=0; i&lt;connections.length; i++) {
  // do something with connections[i]    // (3)
}
</code></pre>

<p>Question: Is there a risk that the socket's <code>disconnect</code> event will delete an item <code>(2)</code> from the <code>connections</code> object while the loop is running <code>(3)</code> and throw an error?  If so, what's the best practice?</p>

<p>I can "mark it" to be deleted and only remove it after the loop completes, but is it necessary?  Thanks!</p>