javascript 计算数组的位/字节大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19354808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:17:58  来源:igfitidea点击:

Count bit/byte size of array

javascriptjquerybyte

提问by lawls

I have an array in Javascript that has lot of sub arrays. What would be the best/simplest way to count how many bits/bytes the array holds? I'm gonna send the array to my PHP server, and it can only be 5kB big.

我在 Javascript 中有一个数组,它有很多子数组。计算数组包含多少位/字节的最佳/最简单方法是什么?我要把数组发送到我的 PHP 服务器,它只能是 5kB 大。

Is there a native method for this? I'm not so very well acquainted with bits. If I understood it correctly 1 character fits in 8b/1B (although it depends on the encoding obviously). Would the best way be to loop through all arrays and count the characters?

是否有本机方法?我对比特不是很熟悉。如果我理解正确,1 个字符适合 8b/1B(尽管它显然取决于编码)。最好的方法是遍历所有数组并计算字符数吗?

采纳答案by Kna?is

First you have to convert the array to the string representation used to transmit the data to the server. The size of the array does not matter since it will be that string that will be transmitted. Depending on the way you serialize that array, the size difference can be very significant.

首先,您必须将数组转换为用于将数据传输到服务器的字符串表示形式。数组的大小无关紧要,因为它将是要传输的字符串。根据序列化该数组的方式,大小差异可能非常显着。

Possible options to serialize the array are jQuery.param()method or JSON.stringify(). You can also build your own method that converts the values so that your PHP code can understand it.

序列化数组的可能选项是jQuery.param()method 或JSON.stringify()。您还可以构建自己的方法来转换值,以便您的 PHP 代码可以理解它。

After you have that string, you take stringValue.length * 8to get the size, assuming that a) the values are ASCII or b) you url-encoded all values so that Unicode characters are transformed into ASCII. *8is there to get the bits, but since you mention that the limit is 5kB - those most probably are bytes so you skip the multiplication.

获得该字符串后,您可以stringValue.length * 8获取大小,假设 a) 值是 ASCII 或 b) 您对所有值进行了 url 编码,以便 Unicode 字符转换为 ASCII。*8是否可以获取这些位,但是由于您提到限制为 5kB - 这些很可能是字节,因此您可以跳过乘法。

回答by Royi Namir

You can do this : ( in order to get realy good estimation)

你可以这样做:(为了得到真正好的估计)

var g = JSON.stringify(myBigNestedarray).replace(/[\[\]\,\"]/g,''); //stringify and remove all "stringification" extra data
alert(g.length); //this will be your length.

For example : have alook at this nested array :

例如:看看这个嵌套数组:

var g=[1,2,3,['a','b',['-','+','&'],5],['????','????']]

var g=[1,2,3,['a','b',['-','+','&'],5],['????','????']]

JSON.stringify(g).replace(/[\[\]\,\"]/g,'')

JSON.stringify(g).replace(/[\[\]\,\"]/g,'')

 "123ab-+&5????????"

Which its length is : 17 (bytes)

它的长度是:17(字节)

This way - you use the benefit of json.parsewhich do most of the job - and then remove the extra array holders.

这样 - 您可以利用其中的优势json.parse来完成大部分工作 - 然后移除额外的阵列支架。

回答by P Roitto

You can use the Blobto get the arrays size in bytes when it's converted in to a string.

当它转换为字符串时,您可以使用Blob获取以字节为单位的数组大小。

Examples:

例子:

const myArray = [{test: 1234, foo: false, bar: true}];

console.info(new Blob([JSON.stringify(myArray)]).size); // 38