Canatron Canatron - 2 months ago 18
Javascript Question

How much data does a string data type take up?

Very simple question, how much data (bytes) do strings take up? Do they take up 1 byte per character?

I tried searching it up, but ws schools doesn't say...

I want to know this to reduce bandwidth in my web app.

Also, for anyone that knows, does socket.io automatically json stringify when using

socket.emit()
;?

Answer Source

String is a character array. So, it will take up roughly sizeof(char) * noOfCharacters ignoring other fields in String class for now. Character can be of 1 byte or 2 bytes depending upon the system, the type of chars being represented- unicode etc. However, from your question, you are more interested in data being transported over the network. Note that data is always exchanged in bytes (byte[]) and thus string will be converted into byte[] representation first and then ported over. To limit the bandwidth usage, you can enable compression, choose interoperable serialisation technique(protobuf, smile, fastinfoset etc)