Transpiling your boolean payload
Alwar G
Posted on April 1, 2021
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');
}
Code 2:
let isPassed = 1;
if (isPassed) {
console.log('Passed');
}
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
}]
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
}]
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:
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 🔥.
Here I expressed my thoughts. Kindly post your comments if you want to share something. Thanks for reading the post 😍.
Posted on April 1, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.