When using Buffers, do the read methods also use the same initial Buffer heap memory?

23 views
Skip to first unread message

Ryan Cole

unread,
May 15, 2012, 5:35:21 PM5/15/12
to nod...@googlegroups.com
I'm writing a module that parses data as it comes in from a tcp socket. When reading out data from a Buffer, is it better to stick to the slice method because of the fact that it uses the same heap memory as the initial Buffer? Do the read methods do this, as well? I've just assumed that the read methods do not use that same Buffer memory. I'd think that if it were possible to stick to just using slice, my module would use less memory during this data parsing phase. Is this accurate?

Thanks,
Ryan

Ben Noordhuis

unread,
May 15, 2012, 6:05:34 PM5/15/12
to nod...@googlegroups.com
On Tue, May 15, 2012 at 11:35 PM, Ryan Cole <ry...@rycole.com> wrote:
> I'm writing a module that parses data as it comes in from a tcp socket. When
> reading out data from a Buffer, is it better to stick to the slice method
> because of the fact that it uses the same heap memory as the initial Buffer?

Yes.

> Do the read methods do this, as well? I've just assumed that the read
> methods do not use that same Buffer memory. I'd think that if it were
> possible to stick to just using slice, my module would use less memory
> during this data parsing phase. Is this accurate?

I'm not sure what read methods you mean. socket.read()?
buf.readUInt8() and friends?

Ryan Cole

unread,
May 15, 2012, 6:35:59 PM5/15/12
to nod...@googlegroups.com
Yea, I meant the buf.readUInt32BE and friends.

Thanks,
Ryan

billywhizz

unread,
May 15, 2012, 7:13:24 PM5/15/12
to nod...@googlegroups.com
the read methods are looking at the same memory. they just index into a v8 array that is wrapped around the raw memory buffer for convenience and speed of access. having said that, there are a lot of asserts and checks in the read* methods so i'd imagine they introduce quite a lot of overhead. as for using less memory, i don't see how using the read* methods will make any difference to memory usage.

mscdex

unread,
May 15, 2012, 7:26:14 PM5/15/12
to nodejs
On May 15, 7:13 pm, billywhizz <apjohn...@gmail.com> wrote:
> the read methods are looking at the same memory. they just index into a v8
> array that is wrapped around the raw memory buffer for convenience and
> speed of access. having said that, there are a lot of asserts and checks in
> the read* methods so i'd imagine they introduce quite a lot of overhead. as
> for using less memory, i don't see how using the read* methods will make
> any difference to memory usage.

You can skip the assertions by passing `true` as the second argument
to the read* methods.
Reply all
Reply to author
Forward
0 new messages