Count bit/byte size of array

2020-08-14 10:24发布

问题:

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.

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:

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.

After you have that string, you take stringValue.length * 8 to 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. *8 is there to get the bits, but since you mention that the limit is 5kB - those most probably are bytes so you skip the multiplication.



回答2:

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],['שלום','יגבר']]

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

 "123ab-+&5שלוםיגבר"

Which its length is : 17 (bytes)

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



回答3:

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

Examples:

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

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