Count bit/byte size of array

20,919

Solution 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.

Solution 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.

Solution 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
Share:
20,919

Related videos on Youtube

lawls
Author by

lawls

Updated on March 17, 2020

Comments

  • lawls
    lawls over 4 years

    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?

  • Prinzhorn
    Prinzhorn over 10 years
    Quoting jQuery.param docs: "Note: Because there is no universally agreed-upon specification for param strings, it is not possible to encode complex data structures using this method in a manner that works ideally across all languages supporting such input. Use JSON format as an alternative for encoding complex data instead."
  • Royi Namir
    Royi Namir over 10 years
    The op wants to know how many bytes the array holds. your solution doesnt consider the extra chars which represents arrays [] and " etc... so according to your solution -- [1] has length of 3 which is incorrect ( it has a length of 1).
  • Prinzhorn
    Prinzhorn over 10 years
    @RoyiNamir That's what he said, but I don't think it's what he meant. If he only can transfer 5kB to the server, then the length of the serialized data counts. And [1] has a length of 8, because JavaScript stores the 1 as 64 bit IEEE 754 floating point number. You see, his question is ambiguous.
  • Royi Namir
    Royi Namir over 10 years
    @Prinzhorn I read : "how many bytes the array holds." that's my answer was about. :-)
  • Prinzhorn
    Prinzhorn over 10 years
    @RoyiNamir I read : "I'm gonna send the array to my PHP server" that's what my answer was about. :-). But enough of it ;)
  • exside
    exside almost 4 years
    careful with this, I think it's quite elegant for single use, but I ran into massive memory leaks when using this to "measure" the size of an Array I use as a buffer for very fast incoming websocket data (measured each 5 seconds as it grows)...it completely blew up the memory usage from 25MB at the beginning to 1GB within minutes...very strange
  • OhadR
    OhadR about 2 years
    loved the 'שלוםיגבר'