'Does the Conditional (ternary) operator in JavaScript (used in many places as error prevention) place excessive strain on a controller/memory?
I'm adding some stability to my code and I want to make sure I handle null and undefined behavior. Usually I toss in some ternary logic to make sure I don't error out, but I'm wondering if this puts any unnecessary strain on the controller?
For example:
let inactiveCount = totalCount - activeCount;
vs
let inactiveCount = (totalCount ? totalCount : 0) - (activeCount ? activeCount : 0);
I would add in the logic like so to make sure I'm not trying to do an operation on null values. Now obviously this is an example that doesn't require this type of logic if I just format my code such that those values are never null, but this is just an arbitrary example of how I would be adding the logic in on values that potentially could be null if the backend scripting has any issues. There are queries writing some metrics, metrics that I'll be using in my controller, and I can't guarantee those values always exist because of the data I have to work with.
So to sum it up, my question is this: does this logic put much strain, if any, on the controller?
All the data is mutually exclusive. Otherwise, I'd just have a handler function that defaults all the values once before going to the logic function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
