Short circuit vs addition

JavaScript performance comparison

Test case created by cwgorman

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let total = 0;

};

Benchmark.prototype.teardown = function() {
  console.log(total);

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Short-circuit
const a = Math.round(Math.random());
const b = Math.round(Math.random());
const c = Math.round(Math.random());

if (!a && !b && !c) {
  total++;
}
pending…
Addition
const a = Math.round(Math.random());
const b = Math.round(Math.random());
const c = Math.round(Math.random());

if (a + b + c === 0) {
  total++;
}
pending…
Exact zero
const a = Math.round(Math.random());
const b = Math.round(Math.random());
const c = Math.round(Math.random());

if (a === 0 && b === 0 && c === 0) {
  total++;
}
pending…
Bitwise
const a = Math.round(Math.random());
const b = Math.round(Math.random());
const c = Math.round(Math.random());

if (a | b | c === 0) {
  total++;
}
pending…
OR (DeMorgan)
const a = Math.round(Math.random());
const b = Math.round(Math.random());
const c = Math.round(Math.random());

if (!(a || b || c)) {
  total++;
}
pending…
Inequality comparator
const a = Math.round(Math.random());
const b = Math.round(Math.random());
const c = Math.round(Math.random());

if (!(a >= 1 || b >= 1 || c >= 1)) {
  total++;
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments