Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer vs String speed: Why is String faster?

Tags:

I have this project, called Memcached.Js, which is a port of Memcached server to Node.js.

I've been playing arround with strings and buffers, comparing memory footprint and performance. For memory, there's no question that buffer is the right choice.

But for my surprise the same is not true for performace. Performing string manipulation is faster than using buffer. This is what I tried:

// Option 1: data.toString() - amazing, but it's the best one var commandDataStr = mdata.data.toString().substr(startPos, bytes); var commandData = new Buffer(commandDataStr);  // Option 2: data.slice().toString() - the same as above... What? var commandDataStr = mdata.data.slice(startPos, startPos + bytes).toString(); var commandData = new Buffer(commandDataStr);  // Option 3: data.slice() - bad var commandData = mdata.data.slice(startPos, startPos + bytes);  // Option 4: data.copy() - bad as well var commandData = new Buffer(bytes); mdata.data.copy(commandData, 0, startPos, startPos + bytes); 

The complete code is here: https://github.com/dalssoft/memcached.js/blob/master/lib/memcached.ascii.commands.js#L72

Testing the code: ruby test/from_clients/perf_test.rb

The tests showed that Strings are faster than Buffer. Since it's not what I was expecting, I think I'm probably doing something wrong, but I can't find exactly what it is.

Can anyone help me here?

Tks!

like image 649
David Lojudice Sb. Avatar asked Feb 04 '11 18:02

David Lojudice Sb.


1 Answers

Strings are built-in to V8, and allocate memory within the VM. Buffers were added not to make all string operations faster, but to represent binary data, where as Strings are unicode.

When writing large amounts of data to a socket, it's much more efficient to have that data in binary format, vs having to convert from unicode.

So for common operations, like concat, I'm not surprised Strings are faster.

like image 91
cloudhead Avatar answered Oct 11 '22 22:10

cloudhead