21

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?

lawls
  • 1,498
  • 3
  • 19
  • 34

3 Answers3

18

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.

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
11

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.

Knaģis
  • 20,827
  • 7
  • 66
  • 80
  • 1
    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." – Prinzhorn Oct 14 '13 at 07:01
  • 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). – Royi Namir Oct 14 '13 at 07:02
  • @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. – Prinzhorn Oct 14 '13 at 07:19
  • @Prinzhorn I read : "how many bytes the array holds." that's my answer was about. :-) – Royi Namir Oct 14 '13 at 07:21
  • @RoyiNamir I read : "I'm gonna send the array to my PHP server" that's what my answer was about. :-). But enough of it ;) – Prinzhorn Oct 14 '13 at 07:37
4

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
P Roitto
  • 1,533
  • 1
  • 11
  • 9
  • 3
    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 – exside Sep 17 '20 at 22:38
  • @exside Why? I mean do you have an idea why those leaks happened to you? Are you sure they were due to this? – aderchox Jul 25 '22 at 05:15
  • @aderchox actually not really, would love some insight if someone knows, i basically intended to have some monitoring/stats of my data buffer for incoming data (e.g. to see if my buffer size is large enough or if it overflows as the data is not coming in at the same rate all the time, but then i noticed this memory hogging issue (not even sure if we can call it a "leak")... – exside Jul 26 '22 at 12:14