if inside while

JavaScript performance comparison

Test case created by Cezary Tomczyk

Preparation code

<div id="test">
  <span>
    <span>
      <span>
        <span>
          <span>
            <span id="last">
            </span>
          </span>
        </span>
      </span>
    </span>
  </span>
</div>
<script>
  var parentElm = document.getElementById('test');
  var nodeElm = document.getElementById('last');

  var contains = function(parent_node, node) {
      while (node) {
        if (node === parent_node) {
          return true;
        }
        node = node.parentNode;
      }
      return false;
      };
  var contains2 = function(parent_node, node) {
      while (node && node !== parent_node) {
        node = node.parentNode;
      }
      if (node === parent_node) {
        return true;
      }
      return false;
      };
</script>
    

Preparation code output

<div id="test"> <span> <span> <span> <span> <span> <span id="last"> </span> </span> </span> </span> </span> </span> </div>

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
inside while
contains2(nodeElm, parentElm);
pending…
outside while
contains(nodeElm, parentElm);
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