Count bit/byte size of array

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.


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

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.