data attributes vs class

JavaScript performance comparison

Test case created by Michael Hoisie

Preparation code

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

<form id="form1">
<input class="field1"></input>
<input class="field2"></input>
<input class="field3"></input>
<input class="field4"></input>
<input class="field5"></input>
</form>

<form id="form2">
<input data-field="field1"></input>
<input data-field="field2"></input>
<input data-field="field3"></input>
<input data-field="field4"></input>
<input data-field="field5"></input>
</form>

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
Test class accessor
var a = $("#form1 .field1").val();
var b = $("#form1 .field2").val();
var c = $("#form1 .field3").val();
var d = $("#form1 .field4").val();
var e = $("#form1 .field5").val();
 
pending…
Test data accessor
var a = $("#form2 [data-field=field1]").val();
var b = $("#form2 [data-field=field2]").val();
var c = $("#form2 [data-field=field3]").val();
var d = $("#form2 [data-field=field4]").val();
var e = $("#form2 [data-field=field5]").val();
 
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