title: Best way to add an event listener on socket.io to save resources
body: <p>So i'm trying to know how javascript handles function.</p>
  <p>if i have a code like this:</p>
  <pre><code>io.on(&quot;connection&quot;, function(socket) {
    socket.on(&quot;hi&quot;, function(data) {
      socket.emit(&quot;emit&quot;, &quot;hey&quot;)
    })
  })
  </code></pre>
  <p>What's in my mind is that each new connection, javascript will create another function for the &quot;hi&quot; event. So what I'm currently doing in my app is like this:</p>
  <pre><code>function hi(data) {
      this.emit(&quot;emit&quot;, &quot;hey&quot;)
  }
  
  io.on(&quot;connection&quot;, function(socket) {
    socket.on(&quot;hi&quot;, hi)
  })
  </code></pre>
  <p>This way javascript will just reuse the function hi instead of instancing a new one?? I'm not sure if this is necessary but I want to consume less resources as much as possible because I'm thinking about what will happen when the server have thousand of connections.</p>