javascriptnode.jsbuffernode.js-buffer

Canonical way to remove multiple bytes from buffer


Say I have a simple Buffer in Node.js like so:

const bytes = Buffer.from('abcdefg');

this buffer instance has slice and concat as methods, but I am really not sure how to use these to basically create the functionality of pop/shift/splice of an array.

here are the Buffer docs: https://nodejs.org/api/buffer.html

What I am basically looking to do is read/remove the first X number of bytes, like so:

function read(x){

// return the first x number of bytes from buffer
// and remove those bytes from the buffer
// side-effects be damned for the moment
}

here's what I have but it seems pretty "wrong" to me even though it also seems to work:

let items = Buffer.from('abcdefg');

function read(x){
    const b = items.slice(0,x);
    items = items.slice(x,items.length);
    return b;
}

console.log(String(read(4)));
console.log(String(items));

Is there a better way to do this?

also, I am not sure if read is the right word, but pop would connote an array...what's the right word to use the describe what this function does?


Solution

  • From the Node.js v10.x API docs (bolded by me):

    Instances of the Buffer class are similar to arrays of integers but correspond to fixed-sized, raw memory allocations outside the V8 heap. The size of the Buffer is established when it is created and cannot be resized.

    Which is why there is no .pop() method for Buffer because it isn't an operation for something that is fixed-size in nature unlike an array. Same goes for shift and splice. You can't extend the already allocated Buffer but you can create new ones.

    Using .slice() won't give you a new Buffer, instead, returns a subset of the memory occupied by the original Buffer. While that approach works, there could be a possibility that some other variable still references the original Buffer in which case, modifications made to the subset you got from .slice() could carry over to the original Buffer as well.

    Given the nature of Buffer and the kind of operations you seem to want, it's best to first convert items into a string. You can then perform all the operations you mentioned by splitting using .split(''). Once you're done, you can join the split string and create a new Buffer using Buffer.from(string) and assign it back to items. This way, your code will be much more clear.