0

In javascript I have seen the usage of

if (!!foo.bar) {
    doSomething();
}

What is the performance and other differences between this and

if (Boolean(foo.bar)) {
    doSomething();
}

?

Is there any overhead for the use of constructor typecasting over negation?

Another (better) example is:

doSomethingWithBoolean(!!foo.bar);
0xc0de
  • 8,028
  • 5
  • 49
  • 75
  • If you want to know the performance difference, go to jsperf.com. – Barmar Jul 24 '13 at 07:22
  • I would just write `if (foo.bar)` instead of either of them. – Barmar Jul 24 '13 at 07:23
  • @Barmar, I actually changed the example, it fits better in doSomething(!!foo.bar) where doSomething expects a boolean. – 0xc0de Jul 24 '13 at 07:25
  • @Close voters: Care to explain? – 0xc0de Jul 26 '13 at 05:23
  • I voted to close because you didn't make any attempt to solve the problem yourself before asking. We're not here to run a trivial benchmark for you. – Barmar Jul 26 '13 at 05:27
  • @barmer: It's asking more than just performance benchmarks. Those things one either knows or doesn't know. I am not aware of how to show 'efforts/attempts' one makes to know. For the record, I have tried running these cases for functions accepting booleans. – 0xc0de Jul 26 '13 at 10:23
  • Other than performance, the answers would just be opinions, and opinion-based questions are frowned upon. – Barmar Jul 26 '13 at 14:24
  • On what basis can one you say that other answers will be just opinions? Maybe post that proof as answer here? And even if they are, I am asking question because I don't know the answer. Is anyone else who stumbles upon the same Q, supposed to know that there are no other differences other than performance? – 0xc0de Jul 26 '13 at 17:07

4 Answers4

1

I dont think there will be any significant performance issue. !!foo.bar is doing same thing with less typing.

With boolean(foo.bar) there might be very-very small performance issue as this is using constructor but with today's hardware that is ignorable.

After all I will simply use foo.bar or !foo.bar

Manish Kumar
  • 10,214
  • 25
  • 77
  • 147
0

The double negation is just quicker to type and makes for shortened files. It’s a common compression technique.

It could be (a tiny little bit) slower, but since it’s an ubiquitous shortcut I guess there’s a compiler optimization. Don’t nitpick over such implementation details!

Iso
  • 3,148
  • 23
  • 31
0

By calling Boolean(foo.bar) , you are getting a wrapped boolean object which would have some additional Object sspecific functionality (and here you dont want them)

Using !!foo.bar you'll get a boolean literal, So i think this evaluation will be fast.

schnill
  • 905
  • 1
  • 5
  • 12
-2

In Javascript, with

if (Boolean(foo.bar)) {
    doSomething();
}

doSomething will execute since Boolean() create a not null object, even if it is Boolean(false). "Boolean" returns an object, "!!" returns a boolean value, e.g. true/false. My source for telling this is here.

PS: Boolean, with a caps at the beginning, is the Javascript object

Ricola3D
  • 2,402
  • 17
  • 16
  • 1
    What you are saying happens when you do new Boolean() – 0xc0de Jul 24 '13 at 07:30
  • 1
    @Ricola3D—better fix your answer, it's inconsistent with [EMCA-262](http://ecma-international.org/ecma-262/5.1/#sec-15.6.1.1): `Boolean(value) Returns a Boolean value (not a Boolean object) computed by ToBoolean(value)`. – RobG Jul 24 '13 at 08:15