Buffer vs String

86 views
Skip to first unread message

Michael Monashev

unread,
Sep 14, 2014, 8:52:36 PM9/14/14
to nod...@googlegroups.com
Hi!

I rewite some code from Strings to Buffers with zero data copying (I
use slice). But the new code was 10 times slower! I Buffer slower than
String?

--
Michael

Floby

unread,
Sep 15, 2014, 4:25:19 AM9/15/14
to nod...@googlegroups.com, softs...@gmail.com
Barrier crossing between JS and C++ is slow. Buffer is implemented by Node.js and therefore each call to .slice() is expensive because of that barrier crossing.
I advise you measure and choose the best option for your use case.

mscdex

unread,
Sep 15, 2014, 12:27:01 PM9/15/14
to nod...@googlegroups.com, softs...@gmail.com

It'd be helpful if you also posted the relevant code.

Andrey

unread,
Sep 16, 2014, 12:33:47 AM9/16/14
to nod...@googlegroups.com, softs...@gmail.com
Strings are very optimized in v8, so just replacing hot path string with buffer does not always help - benchmark first, ideally at a very low level ( statistical profiler / dtrace / generated assembly / irhydra ). For example, if you do `content += chunk` in a loop you are likely not copying strings - internally v8 keeps "content" string as linked list of chunks. Because this is low at code generation path, if you try to emulate this linked list manually in "user space" you might get slower code  
Reply all
Reply to author
Forward
0 new messages