code like SD (senior | shit) Developer - the choice is yours!
This is one of those code reviews where I am not sure if I shall cry or call for more ice-creams to cool down.
Someone was tasked with a function that is adding a checkbox to the dataTable and then making an API call with selected Ids to the backend.
A trivial task we can think, but not as always.
Let see a bit of simplified frontend code, where rows selected are taken form checkboxes plugin for dataTables and that comes as object so it need to be iterated to get values and store them in new place. In this particular case an engineer decided to push it to comm separated values, the assign to model and push to an api endpoint.
Is this bad - hmm it depends - if we will use this string as a query parameter - that is fantastic (no JSON conversion needed on client and server side), but pushing it to string as an model is a no, no.
Why?
1. performance overhead on client and server
2. native form change (array of integers to string) - that need to be redone on an api side
3. now see the object where that was passed:
So what is performance overhead in the client code? https://jsbench.me/ provides a very simple and easy benchmark harness that allows for a quick performance checks.
Unfortunately their dark theme sucks as a hell
With 10 ids we are having 15% less compute (in this case payload on wire will be similar).
I need to admit that I was in shock when I found the results.
So the obscured model processing code looks like this:
- We are wasting user time in a processing overhead
- The transform array to string and back adds extra lines of code to maintain that means a longer time is needed to understand it (yeah know 3 seconds :-P) but we are reading code 20 times before we add another line
- Api as a cloud service consumes precious CPU ticks so in fact our $$
- If we can reduce compute in an easy way - then we also emit less heat and consume less energy (and all the eco shit here).
Comments
Post a Comment