Google Groups

Re: [node-dev] Safe to use socket.ondata instead of on('data')?


Jann Horn Apr 19, 2012 9:49 AM
Posted in group: nodejs-dev
On Thu, Apr 19, 2012 at 09:43:56AM -0700, Joran Greef wrote:
> I spotted this in the source of net.js:
>
> // Optimization: emit the original buffer with end points
>     if (self.ondata) self.ondata(buffer, offset, end);
>
> Is it safe to use ondata to avoid the buffer.slice overhead introduced when
> otherwise calling on('data') callbacks?

If you're the only one listening on the socket, I think it should be no problem. However, as soon
as someone else starts using .on('data') on the same socket, your speed advantage is gone, and as
soon as someone else tries to also use .ondata (or you try to use it twice) on the same socket, the
old callback gets kicked out. So, it might be a good idea if you're sure you're the only one messing
with the socket.