Skip to main content
Ben Nadel at InVision In Real Life (IRL) 2019 (Phoenix, AZ) with: Winnie Tong
Ben Nadel at InVision In Real Life (IRL) 2019 (Phoenix, AZ) with: Winnie Tong ( @wintopia )

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

By
Published in

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.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel