NodeJS 101: Use Buffer to Append Strings Instead of Concatenating Them

by Clint on November 28, 2011

Update (1/7/12): According to Ben Noordhuis, one of the core Node.js contributors and author of the buffertools module, you don’t really need to worry about using buffers when it comes to concatenating strings. Apparently V8 is extremely clever, so for example, a statement like a += b + c actually results in no copying whatsoever.

Andrew Maurer recently shared some test results showing how much more efficient it is to use Buffers when building strings in NodeJS. Although the value behind using buffers to concat strings was drilled into me over a decade ago while using Java, I’m embarrassed to admit it hadn’t occurred to me in my NodeJS work. I think it’s because, like most people, I picked up JavaScript a long time ago in the context of browser environment–a place where nobody really uses string buffers, and there aren’t any built-in mechanisms for doing so. With that in mind, I thought it was worth sharing this personal facepalm as reminder to others. It’s “101″ stuff that every Node developer should know.

To make things easier, I recommend using a module called buffertools (github / npm) which augments the out-of-box Buffers object and makes it super-easy to efficiently append strings (note that it was written by one of the core NodeJS deveopers). To borrow from the project’s own documentation, here’s an example:

require('buffertools');

// The next three lines are identical to doing "new Buffer('foobarbaz')"
a = new Buffer('foo');
b = new Buffer('bar');
c = a.concat(b, 'baz'); // non-standard function added by buffertools

console.log(a, b, c); // "foo bar foobarbaz"

// static variant
buffertools.concat('foo', new Buffer('bar'), 'baz');

Note that buffertools won’t work out-of-box on Windows since it uses UNIX-only native code.

Clint Harris is an independent software consultant living in Brooklyn, New York. He can be contacted directly at ten.sirrahtnilc@tnilc.
  • Andrew Maurer

    Thanks for the link-back Clint! This module looks pretty cool, I’ll have it give it a go.

  • Anonymous

    I think buffertools has windows support now. Please check and update your post.