3

I want to use a buffer to store a sparse index. If I allocate a buffer of, say, 1024 bytes, and store effectively 128 bytes in it, how many bytes will be allocated in memory?

3
  • What are you calling a buffer ? a string ? a byte array ? Commented Sep 28, 2015 at 8:15
  • nodejs.org/api/buffer.html Commented Sep 28, 2015 at 8:16
  • 1
    that may be not a part of v8 but of node js Commented Sep 28, 2015 at 8:16

1 Answer 1

1

According to the constructor doc:

new Buffer(size)#

size Number Allocates a new buffer of size bytes. size must be less than 1,073,741,824 bytes (1 GB) on 32 bits architectures or 2,147,483,648 bytes (2 GB) on 64 bits architectures, otherwise a RangeError is thrown.

Unlike ArrayBuffers, the underlying memory for buffers is not initialized. So the contents of a newly created Buffer are unknown and could contain sensitive data. Use buf.fill(0) to initialize a buffer to zeroes.

It seem that the whole memory is allocated, but not zeroified.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I have read that, but I was thinking of a possible memory allocation optimisation. If not I will do my own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.