Functions in JavaScript are considered as objects, and as such, they are of reference type. We can tell by doing a simple test like this:
const a = () => {};
const b = () => {};
const c = a;
console.log(a === b); // false
console.log(a === c); // true
Whenever a new connection is created in your case, a reference of the function is passed as the callback, which means that every connection would be calling the same memory address where the function was initially stored. So your implementation is correct, you don't have to worry about the possibility of using huge amounts of memory in this case :-)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…