jQuery ID selector performance (v3)

Revision 3 of this benchmark created on


Description

Given an #id selector I was curious to see just how big a difference it would make to save the jQuery object instead of re-instantiating it over and over.

Read the full blog post, jQuery ID selector performance.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="my-id" class="my-class"></div>

Setup

$elem = $("[id]");

Test runner

Ready to run.

Testing in
TestOps/sec
ID selector
var x = $('#my-id').length + Math.random();
ready
Class Selector
var x = $('.my-class') + Math.random();
ready
ID and tag selector
var x = $('div#my-id').length + Math.random();
ready
DOM element
var x = $(document.getElementById('my-id')).length + Math.random();
ready

Revisions

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