minimize jQuery object creation

JavaScript performance comparison

Revision 9 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="appRoot">
  <div class="element1">
    element1
    <p class="el_p">
      additional tag
      <span class="el_s">
        and another
      </span>
    </p>
  </div>
  <div class="element2">
    element2
    <p class="el_p">
      additional tag
      <span class="el_s">
        and another
      </span>
    </p>
  </div>
  <div class="element3">
    element3
    <p class="el_p">
      additional tag
      <span class="el_s">
        and another
      </span>
    </p>
  </div>
  <div class="element4">
    element4
    <p class="el_p">
      additional tag
      <span class="el_s">
        and another
      </span>
    </p>
  </div>
  <div class="element5">
    element5
    <p class="el_p">
      additional tag
      <span class="el_s">
        and another
      </span>
    </p>
  </div>
  <div class="element6">
    element6
    <p class="el_p">
      additional tag
      <span class="el_s">
        and another
      </span>
    </p>
  </div>
</div>

Preparation code output

element1

additional tag and another

element2

additional tag and another

element3

additional tag and another

element4

additional tag and another

element5

additional tag and another

element6

additional tag and another

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
More jQuery objects
$(function() {

  $(".element1").bind("click", function() {
    $(".element2").text("clicked by 1");
  })

  $(".element3").bind("click", function() {
    $(".element1").text("clicked by 3");
  })
  $(".element6").bind("click", function() {
    $(".element1").text("clicked by 3");
  })
  $(".element4").bind("click", function() {
    $(".element5").text("clicked by 4");
  })

  $(".element1").click();
  $(".element2").click();
  $(".element3").click();
  $(".element4").click();
  $(".element5").click();
  $(".element6").click();
});
pending…
Less jQuery Objects
$(function() {

  var appRoot = $("#appRoot");


  appRoot.children(".element1").bind("click", function() {
    appRoot.children(".element2").text("clicked by 1");
  })

  appRoot.children(".element3").bind("click", function() {
    appRoot.children(".element1").text("clicked by 3");
  })


  appRoot.children(".element6").bind("click", function() {
    appRoot.children(".element1").text("clicked by 3");
  })


  appRoot.children(".element4").bind("click", function() {
    appRoot.children(".element5").text("clicked by 4");
  })

  $(".element1").click();
  $(".element2").click();
  $(".element3").click();
  $(".element4").click();
  $(".element5").click();
  $(".element6").click();
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment