David Lojudice Sb. David Lojudice Sb. - 9 months ago 61
Javascript Question

Buffer vs String speed: Why is String faster?

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:

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?


Answer Source

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.