Example - selector caching

JavaScript performance comparison

Test case created by Cezar "ikari" Pokorski

Info

This is an example test to show JSPerf capabilities and also to show why caching jQuery selector results outside of a loop is much faster. And how much.

Preparation code

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var $p = $('p');
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Lookup every time
    $('p').css('color','#000000');
pending…
Cached lookup
    $p.css('color','#000000');
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

1 comment

Cezar commented :

The newer Firefox, the slower...

Add a comment