class-vs-data

JavaScript performance comparison

Revision 10 of this test case created

Info

Class selector vs Data attribute selector

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<div class="test" data-test="test"></div>
<script>
Benchmark.prototype.setup = function() {
    (function(){
   
        var matcher = /\s*(?:((?:(?:\\\.|[^.,])+\.?)+)\s*([!~><=]=|[><])\s*("|')?((?:\\\3|.)*?)\3|(.+?))\s*(?:,|$)/g;
   
        function resolve(element, data) {
   
            data = data.match(/(?:\\\.|[^.])+(?=\.|$)/g);
   
            var cur = jQuery.data(element)[data.shift()];
   
            while (cur && data[0]) {
                cur = cur[data.shift()];
            }
   
            return cur || undefined;
   
        }
   
        jQuery.expr[':'].data = function(el, i, match) {
   
            matcher.lastIndex = 0;
   
            var expr = match[3],
                m,
                check, val,
                allMatch = null,
                foundMatch = false;
   
            while (m = matcher.exec(expr)) {
   
                check = m[4];
                val = resolve(el, m[1] || m[5]);
   
                switch (m[2]) {
                    case '==': foundMatch = val == check; break;
                    case '!=': foundMatch = val != check; break;
                    case '<=': foundMatch = val <= check; break;
                    case '>=': foundMatch = val >= check; break;
                    case '~=': foundMatch = RegExp(check).test(val); break;
                    case '>': foundMatch = val > check; break;
                    case '<': foundMatch = val < check; break;
                    default: if (m[5]) foundMatch = !!val;
                }
   
                allMatch = allMatch === null ? foundMatch : allMatch && foundMatch;
   
            }
   
            return allMatch;
   
        };
   
    }());
};
</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
Data selector
$(':data(test==test)')
pending…
Data selector
$("[data-test]")
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