最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Canonical way to remove multiple bytes from buffer - Stack Overflow

matteradmin3PV0评论

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: .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?

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/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?

Share Improve this question edited Jan 16, 2017 at 3:21 Alexander Mills asked Jan 16, 2017 at 1:58 Alexander MillsAlexander Mills 100k166 gold badges537 silver badges916 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14 +50

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.

Post a comment

comment list (0)

  1. No comments so far