DEV Community

Cover image for Transpiling your boolean payload
Alwar G
Alwar G

Posted on

Transpiling your boolean payload

Hi,

Suddenly I got thinking of why we have to send the boolean values in our response?πŸ€” You can ask something like what? Are you mad?
I am also thought the same πŸ˜‚.
Consider the below javascript codes

code 1:

let isPassed = true;
if (isPassed) {
    console.log('Passed');
}
Enter fullscreen mode Exit fullscreen mode

Code 2:

let isPassed = 1;
if (isPassed) {
    console.log('Passed');
}
Enter fullscreen mode Exit fullscreen mode

As a web developer, we know that both will give the same output.
My know programming languages parse the 0 as false and 1 as true.
So, I thought no need to give your payload with the boolean values instead of 0s and 1s.
Consider the below the network call

Request:

https://x8nq67.deta.dev/superheroes

Response:

[{
    name: 'Iron Man',
    is_male: true,
    is_idiot: false,
    is_millionaire: true,
    is_inventor: true,
    is_from_avengers: true,
    has_parent: false,
    has_disease: false,
    has_children: true,
    is_best: true,
    has_passion: true
  }, {
    name: 'Super Man',
    is_male: true,
    is_idiot: false,
    is_millionaire: false,
    is_inventor: false,
    is_from_avengers: false,
    has_parent: true,
    has_disease: false,
    has_children: false,
    is_best: true,
    has_passion: false
  }, {
    name: 'Bat Man',
    is_male: true,
    is_idiot: false,
    is_millionaire: false,
    is_inventor: true,
    is_from_avengers: false,
    has_parent: true,
    has_disease: false,
    has_children: false,
    is_best: true,
    has_passion: true
  }]
Enter fullscreen mode Exit fullscreen mode

In the above response, we have multiple boolean values. Let's think of the above network call as below

Request:

https://x8nq67.deta.dev/noboolean/superheroes

Response:

[{
    name: 'Iron Man',
    is_male: 1,
    is_idiot: 0,
    is_millionaire: 1,
    is_inventor: 1,
    is_from_avengers: 1,
    has_parent: 0,
    has_disease: 0,
    has_children: 1,
    is_best: 1,
    has_passion: 1
  }, {
    name: 'Super Man',
    is_male: 1,
    is_idiot: 0,
    is_millionaire: 0,
    is_inventor: 0,
    is_from_avengers: 0,
    has_parent: 1,
    has_disease: 0,
    has_children: 0,
    is_best: 1,
    has_passion: 0
  }, {
    name: 'Bat Man',
    is_male: 1,
    is_idiot: 0,
    is_millionaire: 0,
    is_inventor: 1,
    is_from_avengers: 0,
    has_parent: 1,
    has_disease: 0,
    has_children: 0,
    is_best: 1,
    has_passion: 1
  }]
Enter fullscreen mode Exit fullscreen mode

Here we can replace boolean values with 0s and 1s.

Why we have to give the values like this?

The size of the first response(without 0s and 1s) is 637 Bytes. The size of the second response(with 0s and 1s) is 532 Bytes. So we can reduce the size of the payload to 105(637-532) Bytes. If the payload size is less, then the speed of response is also good.

Output Comparision:

payload-comparision
As we see, the second response is 0.05 seconds faster than the first response. This is a small example. But If the application has more requests, it will give a good impact.
For example, consider your application has 100 requests like above. If we follow the above method, then each response will be 0.05 seconds faster than the previous one. That means your application will be 5 seconds(100 requests * 0.05 seconds) faster than the previous one πŸ”₯.
yes-image
Here I expressed my thoughts. Kindly post your comments if you want to share something. Thanks for reading the post 😍.

Top comments (0)