select by class or data

JavaScript performance comparison

Test case created

Preparation code

<p class="test-node" data-test="node">
I'm the test node.
</p>
<script>
Benchmark.prototype.setup = function() {
    var all_nodes = document.getElementsByTagName("*");
};
</script>

Preparation code output

I'm the test node.

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Test for hit with data attr
for (var i=0, max=all_nodes.length; i < max; i++) {
     var is_test_node = Boolean(all_nodes[i].getAttribute && all_nodes[i].getAttribute('data-test'));
}
pending…
Test for hit with classname
var regex = new RegExp('\\btest-node\\b');
for (var i=0, max=all_nodes.length; i < max; i++) {
     var is_test_node = Boolean(all_nodes[i].className && regex.test(all_nodes[i].className));
}
pending…
Test data with dataset
for (var i=0, max=all_nodes.length; i < max; i++) {
     var is_test_node = Boolean(all_nodes[i].dataset && all_nodes[i].dataset.test);
}
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

0 comments

Add a comment