如何计算nodej中数组的实际字节大小?

发布于 2025-01-18 03:54:44 字数 637 浏览 1 评论 0原文

我正在在我的节点应用程序中进行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事 2025-01-25 03:54:44

对于任何类型的值:

const { serialize } = require('v8')

// i.e
const variable = true 

console.log(serialize(variable).byteLength) // 3

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:

const { serialize } = require('v8')

// i.e
const variable = true 

console.log(serialize(variable).byteLength) // 3

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 use Buffer.byteLength(string.toString(), 'utf8'), which would be 4 for true and 5 for false, and for objects, would always be 15 due to [Object, Object], one could also do Buffer.byteLength(JSON.stringify(object), 'utf8'), which would also return the incorrect value of used memory.

多像笑话 2025-01-25 03:54:44

根据 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文