In hacking on the gRaphaĆ«l code base, I came across a recurring pattern syntax pattern I hadn’t encountered in JavaScript before.

Essentially, instead of using this very common conditional construct:

if (a) b;

The author uses:

!a && b;


if (a) b; else c;

Is written:

!a && b; a && c;

I was curious about the performance difference between the styles, and wrote a benchmark to compare them.

I only tested in WebKit nightly 53415, but I found that in every case, the if / if-else construct outperformed the pure-logic construct. The differences in performance are admittedly negligible, so I’ll be sticking to if-else for readability, and I’ll accept the performance benefit as a happy side-effect.


  1. Eli Grey

    It’s likely that if statements can actually be slightly slower when block-scope variables are enabled (by enabling JavaScript 1.7+ for a script).

    Case 1:

    if (foo) {
      let bar = new FooBar;
      bar.baz(2); = bar;

    Case 2:

    var bar;
    foo && (
      bar = new FooBar,
      bar.baz(2), = bar

    I haven’t profiled this code yet, but I’m going to assume that the latter is faster in JavaScript 1.7 and higher.

  2. duncanbeevers

    Please do let me know if you end up profiling this code.

  3. Eli Grey

    Duncan, here’s a profiler I whipped up quickly which also includes sharp scope. Surprisingly, sharp scope performed the worst.

Leave a Comment

Enclose code in <code lang="ruby"></code> if you care.
Preview your comment using the button below.