JavaScript optimizations
There are tons of resources available online to discuss the various optimizations that can be applied to JavaScript. In this section, we will take a look at some of these micro-optimizations and determine how we can take small steps toward making our JavaScript more performant.
Truthy/falsy comparisons
We have all, at some point, written if conditions or assigned default values by relying on the truthy or falsy nature of the JavaScript variables. As helpful as it is most of the times, we will need to consider the impact that such an operation would cause on our application. However, before we jump into the details, let's discuss how any condition is evaluated in JavaScript, specifically an if
condition in this case. As a developer, we tend to do the following:
if(objOrNumber) { // do something }
This works for most of the cases, unless the number is 0, in which case it gets evaluated to false. That is a very common edge case, and most of us catch it anyway. However...