jqeury-selector-test

JavaScript performance comparison

Test case created by Dima

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  var $body = $('body'),
    $div = $('<div class="products"/>', {
      innerHTML: 'products'
    })
    for (var i = 0; i < 1000; i++) {
      $body.append($div)
    }

};
</script>

Preparation code output

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
$(".products")
var $products = $(".products");
pending…
$("div.products")
var $products = $("div.products");
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.

2 Comments

hector commented :

I think what you a missing in this test is some noise...all your divs are the right class, so being extra specific does not gain you anything.

i would test different ratios: 100 good divs against 100 and 1000 divs with other classes.

hector commented :

your test preparation should be done in test preparation and not in setup, otherwise your second test has a larger document to work against. I replicated my tests because running the same test multiple times should give you the same result. still .products is faster.