如何计算nodej中数组的实际字节大小?
我正在在我的节点应用程序中进行AWS OpenSearch进行批量索引,并且由于块的大小太大而失败了。以下是错误
Request size exceeded 104857600 bytes
,因此我需要计算字节中的实际块大小。因此,我尝试了2种低于2的方法,但不确定哪种方法是正确的。
// with Buffer
const str = 'tree'
const obj = [{a:'sas'},{a:'e'}]
console.log(Buffer.from(str).length); ---> 4
console.log(Buffer.from(obj).length); ---> 2
// with object-sizeof
var sizeof = require('object-sizeof')
const str = 'tree'
const obj = [{a:'sas'},{a:'e'}]
console.log(sizeof(str)); ---> 8
console.log(sizeof(obj)); ---> 12
似乎“缓冲区”需要1个字节,而“ object-sizeof”需要2个字节。那么这里是正确的呢?哪种正确的方法可以检查实际对象数组的字节大小? 提前致谢
I'm doing a bulk indexing in AWS OpenSearch within my node application and it's failing because the chunk size are too big. Below is the error
Request size exceeded 104857600 bytes
So I need to calculate the actual chunk size in bytes. So I tried with below 2 ways and not sure which one is correct.
// with Buffer
const str = 'tree'
const obj = [{a:'sas'},{a:'e'}]
console.log(Buffer.from(str).length); ---> 4
console.log(Buffer.from(obj).length); ---> 2
// with object-sizeof
var sizeof = require('object-sizeof')
const str = 'tree'
const obj = [{a:'sas'},{a:'e'}]
console.log(sizeof(str)); ---> 8
console.log(sizeof(obj)); ---> 12
It seems 'Buffer' takes 1 Byte for a char while 'object-sizeof' takes 2 bytes for a char. So what is correct here? And which is the correct way to check the byte size of actual object array?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于任何类型的值:
3 =
0x03 0x01 0x00
标头字节指示值的类型(布尔值是 0x03),后跟一个指示值的字节布尔值(0x01 表示 true,0x00 表示 false)
使用:
Buffer.byteLength(string, 'utf8')
,将强制使用 Buffer.byteLength(string.toString(), 'utf8'),对于true
为 4,对于false
为 5,对于对象,由于[Object, Object]
始终为 15,也可以执行Buffer.byteLength(JSON.stringify(object), 'utf8')
,这将还返回已用内存的错误值。For any type of value:
3 =
0x03 0x01 0x00
Header byte indicating the type of the value (which is 0x03 for a boolean), followed by a byte indicating the value of the boolean (which is 0x01 for true and 0x00 for false)
Using:
Buffer.byteLength(string, 'utf8')
, would force one to useBuffer.byteLength(string.toString(), 'utf8')
, which would be 4 fortrue
and 5 forfalse
, and for objects, would always be 15 due to[Object, Object]
, one could also doBuffer.byteLength(JSON.stringify(object), 'utf8')
, which would also return the incorrect value of used memory.根据 JavaScript 字符串中有多少字节? 的答案,您可以使用 Buffer.byteLength(string, 'utf8') 获取字符串的字节大小。
无论如何,javascript中char的大小是2个字节,所以我猜它与typescript/node.js相同: https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary
According to answers on How many bytes in a JavaScript string? you can use
Buffer.byteLength(string, 'utf8')
to get the byte size of a string.Anyway, the size of a char in javascript is 2 bytes, so i guess it's the same with typescript/node.js : https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary