1

We need to save server bandwith to send and receive json files so instead of this long responses:

(...)
        {
            var serverResponse = JSON.parse(xml.responseText);

             switch(serverResponse.votestatus)
             {
                case 'image_deleted':
                    console.log("image deleted");
                break;

                case 'image_not_found':
                    console.log("image not found");
                break;
             }
        }
(...)

We shorten the strings like this (2 strings are double the size of one):

        {
            var serverResponse = JSON.parse(xml.responseText);

             switch(serverResponse.votestatus)
             {
                //image deleted
                case '1':
                    console.log("image deleted");
                break;
                //image not found
                case '2':
                    console.log("image not found");
                break;
             }
        }

The question is are Strings bigger in size than integers? is this

var a = "1";

bigger in size than this?

var a = 1;
user254883
  • 599
  • 1
  • 5
  • 14
  • 1
    @Praveen The article you've linked is about ASCII values of characters, which is totally irrelevant to what OP has asked. – Teemu Apr 11 '14 at 10:05

4 Answers4

3

For bandwith, the file size matters.

So yes, in a text file 1 is two bytes less than "1".

But are you sure this is worth the effort?

Look at Javascript minifiers and gzip compression first before trying to manually do this.


Okay, going down this road:

If you prefer

{ "voteStatus": 1 }

over

{ "voteStatus" : "1" }

why not

{ "vs": 1 }

or even

[ 1 ]
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • compression will be used too, this makes a difference for 100k users, that's a lot of requests per second – user254883 Apr 11 '14 at 09:53
  • 1
    How is `"1"` less than `1`? We are talking about a JSON response, which is just all text. so `"1"` is 3 chars, compared with `1` is 1 char. – musefan Apr 11 '14 at 09:55
  • 2
    +1... initially I though it was a typo, but then so long went by (with no edit) I was starting to worry you actually believed it – musefan Apr 11 '14 at 09:58
3

This is easy to measure and it should be measured!

Example

Open the debugger in your browser (for example Firebug with Firefox), activate the network tab and then call the REST API with JSON result, for example

https://freegeoip.net/json/www.bp.com

https://freegeoip.net/json/www.bp.com

You will see the content size (here 235 bytes corresponding to the three lines of JSON result).

enter image description here

Note that the HTTP headers will contribute as well to the total received network traffic (here 421 bytes). You can see these sizes by moving the mouse pointer onto the size column to trigger a tooltip.

So as other answers here noted too, there is not only the size of the JSON payload to consider, but also other influences like HTTP headers or transport compression modules.

Community
  • 1
  • 1
mvw
  • 5,075
  • 1
  • 28
  • 34
1

Refering to this post. In javascript memory String takes 2 bytes per character while number takes 8 bytes. This means you have reduced size of parsed JSON object, but if your target is to save bandwidth then in HTTP request/response all objects are sent as strings so bandwidth cannot depend on object type. While trying to save bandwidth: does your server uses gzip?

Community
  • 1
  • 1
Oskars Pakers
  • 699
  • 3
  • 11
0

"it depends".

in memory, single digit strings are shorter (16 bits) (in utf16). number are uint32 (or double for big numbers like floats). that's 32 bits at minimum.

However, you need to "save server bandwidth". So that doesn't matter, everything is sent in "strings", and size depends on the negotiation between server and client. If your http header is correct, utf8 will be used (8 bit), and it will be gzipped.

Moreover, i seen in the code: "JSON.parse(xml.responseText)".

if response are sent in xml, the overhead is quite bulky, like <stuff>1</stuff>

and it is included in a JSON:
{response: "<stuff>1</stuff>"};

Add headers to both xml and json, and you will have plenty stuff. Sending raw text values might be your best option.

roselan
  • 3,755
  • 1
  • 20
  • 20
  • 1
    "might as well be included in a JSON". Seeing that JSON.parse is used, it probably is JSON already. – Thilo Apr 11 '14 at 10:06