If vs. Ternary (v2)

Revision 2 of this benchmark created on


Description

A quick check to see how variable assignment via ternary compares to a traditional if statement.

Preparation HTML

<p id="test">H</p>

<script>
  var x;
  var tl = document.getElementById('test').length;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Set var using if
if ( tl === 1 ) {
x = true;
} else {
x = false;
}
ready
Set var using ternary
x = tl === 1 ? true : false;
ready

Revisions

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