Triple equals vs Double equals (v7)

Revision 7 of this benchmark created on


Description

Check performance of === against ==. This test actually tests adding numbers and strings and assigning them to variables. Testing equality is orders of magnitude faster than the results of this test. The latest version includes actual numbers and strings being added, to avoid the whole thing being optimised away by some compilers. To properly test equality, see a different jsperf or remove the assignments from this one.

Preparation HTML

<script>
  var i1;
  var i2;
  var s1;
  var s2;

function randomInt() {
   var ret = Math.round(Math.random() * 1E9);
   ret + "";
   return ret;
}
function randomIntStr() {
   var ret = Math.round(Math.random() * 1E9);
   ret += "";
   return ret;
}
function randomBool() {
   var ret = Math.round(Math.random() * 1E9);
   ret = (ret%2 == 1);
   return ret;
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
===(int)
i1 = randomInt(); i2 = randomInt();
if (i1 === i2) { i1 = i1 + 1 }
ready
==(int)
i1 = randomInt(); i2 = randomInt();
if (i1 == i2) { i1 = i1 + 1 }
ready
==(string)
s1 = randomIntStr(); s2 = randomIntStr();
if (s1 == s2) { s1 = s1 + "z" }
 
ready
===(string)
s1 = randomIntStr(); s2 = randomIntStr();
if (s1 === s2) { s1 = s1 + "z" }
ready
=== (different type)
i1 = randomInt(); s1 = randomIntStr();
if (i1 === s1) { i1 = i1 + 1 }
ready
== (different type)
i1 = randomInt(); s1 = randomIntStr();
if (i1 == s1) { i1 = i1 + 1 }
ready
=== (bool)
i1 = randomBool(); s1 = randomBool();
if (i1 === s1) { i1 = i1 + 1 }
ready
== (bool)
i1 = randomBool(); s1 = randomBool();
if (i1 == s1) { i1 = i1 + 1 }
ready

Revisions

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