-2

I have a program that calls a back-end service and collects a response. It is collected as follows. We destructure the response object and get the Error and Data.

const { data, error } = response;

If there is no error, the Error object becomes null. Consider the following two options.

Option One:

if (!error) {
// handle error
}

Option Two:

if (error !== null) {
// handle error
}

In Option One, As per this answer, the following conditions are checked.

  1. undefined: if the value is not defined and it's undefined
  2. null: if it's null, for example, if a DOM element not exists...
  3. empty string: ''
  4. 0: number zero
  5. NaN: not a number
  6. false

Is there an order of execution when we call Option 1? How does the internal condition checking happen in Option 1? Based on this, will there be a performance impact (in a granular level), if we use Option 1 instead of Option 2?

Keet Sugathadasa
  • 11,595
  • 6
  • 65
  • 80
  • 5
    async vs speed of a micro optimization? really? – Nina Scholz Mar 08 '19 at 08:53
  • 1
    Also I think you mean something else than `if (!error) { // handle error }` – mplungjan Mar 08 '19 at 08:56
  • 3
    This must be the most micro of premature optimisations I've ever seen. – VLAZ Mar 08 '19 at 08:58
  • Interestingly the destructuring assignment is probably the [slowest part](https://codeburst.io/es6s-function-destructuring-assignment-is-not-free-lunch-19caacc18137) in this example... – Yoshi Mar 08 '19 at 09:02
  • I agree that it's a very micro level optimization. This came up in a Git Pull Request comment. Could I know the reason for downvotes? – Keet Sugathadasa Mar 08 '19 at 09:04
  • If you know that the error is going to be **null**, then Option Two is preferable. After all, you can control what errors to send from the back end so I think this question is not really relevant. – squeekyDave Mar 08 '19 at 09:15
  • @squeekyDave exactly my point. But would it make a difference if we use either option? That's my actual question. Imagine a mission critical application and every micro second is important. If millions of requests are to be processed sequentially on a legacy system, will this affect? – Keet Sugathadasa Mar 08 '19 at 09:22
  • "*If millions of requests are to be processed sequentially on a legacy system, will this affect?*" are we talking a real system or hypotheticals? Because the latter is a useless example. In the former you should profile the system and identify this is a cause of concern before optimising it. As it stands, a *theoretical* increase of productivity might be entirely insignificant and/or immeasurable and/or irrelevant. Furthermore, it could be engine dependent and/or even optimised away on the fly. So, I find it hard to find value in this question. – VLAZ Mar 08 '19 at 10:08
  • @VLAZ, I understand your concern. But I really would like to know whether there is a theoretical performance increase. That's my actual question. In certain applications, even a very insignificant improvement can have an affect when aggregated. – Keet Sugathadasa Mar 08 '19 at 10:32
  • But a theoretical question is pretty useless. Sure *if* you had that system and *if* that was a problem *then* you would need to know this, I agree. But at that point you can profile against both your machine and your configuration (JS engine, stack, environment, etc) to find this information. An answer given *right now* is not going to be of use to you - perhaps the timings are different between browsers. Perhaps the implementation changes *tomorrow*. Moreover, I find an application where *performance of falsey values* is critical to be unlikely to be written in JS to begin with. – VLAZ Mar 08 '19 at 11:03

1 Answers1

1

There is no order in this usage of if statement, JS just performs type conversion to coerce any value to boolean and then evaluates the result, check this link for more details: https://developer.mozilla.org/en-US/docs/Glossary/Falsy

Falsey results:

if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")
if (``)
if (document.all)
// => false

Truthy results:

if (true)
if ({})
if ([])
if (42)
if ("foo")
if new Date())
if (-42)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
// => true