Node.js documentations states that:
Node.js文档声明:
A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.
Buffer类似于整数数组,但对应于V8堆外部的原始内存分配。
Am I right that all integers are represented as 64-bit floats internally in javascript?
我是对的,所有整数在javascript内部表示为64位浮点数吗?
Does it mean that storing 1 byte in Node.js Buffer actually takes 8 bytes of memory?
这是否意味着在Node.js缓冲区中存储1个字节实际需要8个字节的内存?
Thanks
1 个解决方案
#1
2
Buffers are simply an array of bytes, so the length of the buffer is essentially the number of bytes that the Buffer will occupy.
缓冲区只是一个字节数组,因此缓冲区的长度基本上是缓冲区占用的字节数。
For instance, the new Buffer(size) constructor is documented as "Allocates a new buffer of size octets."
Here octets
clearly identifies the cells as single-byte values. Similarly buf[index]
states "Get and set the octet at index. The values refer to individual bytes, so the legal range is between 0x00 and 0xFF hex or 0 and 255."
.
例如,新的Buffer(size)构造函数记录为“分配大小为八位字节的新缓冲区”。这里八位字节清楚地将单元格标识为单字节值。类似地,buf [index]表示“获取并设置索引处的八位字节。值指的是单个字节,因此合法范围介于0x00和0xFF十六进制之间或0和255之间。”。
While a buffer is absolutely an array of bytes, you may interact with it as integers or other types using the buf.read* class of functions available on the buffer object. Each of these has a specific number of bytes that are affected by the operations.
虽然缓冲区绝对是一个字节数组,但您可以使用缓冲区对象上可用的buf.read *类函数作为整数或其他类型与它进行交互。其中每个都具有受操作影响的特定字节数。
For more specifics on the internals, Node just passes the length through to smalloc
which just uses malloc
as you'd expect to allocate the specified number of bytes.
有关内部的更多细节,Node只是将长度传递给smalloc,它只使用malloc,因为您希望分配指定的字节数。
#1
2
Buffers are simply an array of bytes, so the length of the buffer is essentially the number of bytes that the Buffer will occupy.
缓冲区只是一个字节数组,因此缓冲区的长度基本上是缓冲区占用的字节数。
For instance, the new Buffer(size) constructor is documented as "Allocates a new buffer of size octets."
Here octets
clearly identifies the cells as single-byte values. Similarly buf[index]
states "Get and set the octet at index. The values refer to individual bytes, so the legal range is between 0x00 and 0xFF hex or 0 and 255."
.
例如,新的Buffer(size)构造函数记录为“分配大小为八位字节的新缓冲区”。这里八位字节清楚地将单元格标识为单字节值。类似地,buf [index]表示“获取并设置索引处的八位字节。值指的是单个字节,因此合法范围介于0x00和0xFF十六进制之间或0和255之间。”。
While a buffer is absolutely an array of bytes, you may interact with it as integers or other types using the buf.read* class of functions available on the buffer object. Each of these has a specific number of bytes that are affected by the operations.
虽然缓冲区绝对是一个字节数组,但您可以使用缓冲区对象上可用的buf.read *类函数作为整数或其他类型与它进行交互。其中每个都具有受操作影响的特定字节数。
For more specifics on the internals, Node just passes the length through to smalloc
which just uses malloc
as you'd expect to allocate the specified number of bytes.
有关内部的更多细节,Node只是将长度传递给smalloc,它只使用malloc,因为您希望分配指定的字节数。