Skip to main content
Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.

Buffer.slice() Won't Error On Out-Of-Bounds Indices In Node.js

By Ben Nadel on

Yesterday, when I was looking at streaming cached Buffers into HTTP responses in Node.js, I discovered something rather useful - Buffer.slice() won't throw errors when you attempt to slice beyond the bounds of the Buffer. This makes iterating over the Buffer more straightforward as you can iterate in consistently-sized slices without worrying about hitting a boundary at the end.

To see what I mean, we're going to slice off 6-bytes at a time from a given Buffer that does not divide evenly by 6. Furthermore, we're going to iterate such that our "From" index will also go beyond the end-boundary of the Buffer:

var data = new Buffer( "abcdefghijklmnopqrstuvwxyz" );

// Each slice of the Buffer will be 6 bytes, regardless of where we are in the
// iteration of the buffer at large.
var chunkSize = 6;

// As we iterate, notice that our "i" value - our FROM value - is allowed to go a
// whole chunk-size beyond the end-bounds of the buffer.
for ( var i = 0 ; i < ( data.length + chunkSize ) ; i += chunkSize ) {

	// Slice out 6 bytes, with no bounds checking.
	var slice = data.slice( i, ( i + chunkSize ) );

	console.log( "Slice: %s (%d)", slice.toString(), slice.length );

}

As you can see, both our From (i) and our To (i + chunkSize) indices are allowed to progress beyond the end-boundary of the Buffer. And yet, when we run this code, we get the following terminal output:

Slice: abcdef (6)
Slice: ghijkl (6)
Slice: mnopqr (6)
Slice: stuvwx (6)
Slice: yz (2)
Slice: (0)

As you can see, the Buffer.slice() didn't throw any errors. It just returned a smaller (or empty) Buffer when the slice was not fully contained within the bounds of the Buffer. This is a really minor technical aspect; but, I think this behavior will make some Node.js code easier to read and to write.



Reader Comments