Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count bit/byte size of array

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?

like image 580
lawls Avatar asked Oct 14 '13 06:10

lawls


People also ask

How do you find the size of an array in bytes?

To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.

How do you find the size of an array?

We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);

How many bytes is an array in C?

The number of 8 bit bytes that each element occupies depends on the type of array. If type of array is 'char' then it means the array stores character elements. Since each character occupies one byte so elements of a character array occupy one byte each.

How many bytes is a byte array in Java?

Yes, by definition the size of a variable of type byte is one byte. So the length of your array is indeed array.


3 Answers

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.

like image 135
Royi Namir Avatar answered Oct 16 '22 08:10

Royi Namir


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.

like image 28
Knaģis Avatar answered Oct 16 '22 10:10

Knaģis


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
like image 38
P Roitto Avatar answered Oct 16 '22 10:10

P Roitto