0

Which of the following is fastest and what is the justification for why?

(1) if (x == 0 and y == 0)

(2) if ((x | y) == 0)

(3) Other (Please mention. If any)

oreopot
  • 3,392
  • 2
  • 19
  • 28
Donald Knuth
  • 145
  • 4
  • 6
    Knuth should know better.. ;-) – techfoobar Oct 11 '15 at 04:01
  • 2
    1. is not valid javascript. 2. will fail for example `var x = 0, y =0.5; (x|y) == 0` - so "other" , for example `if (x===0 && y === 0)` - definitely will be fastest of the 3 as it's the only valid one – Jaromanda X Oct 11 '15 at 04:01
  • 1
    If you want to compare with the numeric `0` and not just any *falsy* value, you need to use `===` – techfoobar Oct 11 '15 at 04:03
  • I presume you are worried about this because this is in a part of your code which is executed ten million times? –  Oct 11 '15 at 07:10

1 Answers1

2

I would probably use Strict Equality if you want to check they are exactly the same, ie they're the same type too, just in case.

if (x=== 0 && y === 0)

Community
  • 1
  • 1
oreopot
  • 3,392
  • 2
  • 19
  • 28