Custom Data Attributes (data-*) using JavaScript

JavaScript performance comparison

Test case created by Rochas

Info

JavaScriptでカスタムデータ属性にアクセスするにはdataset IDLとgetAttribute() / setAttribute()があり、 またjQueryでカスタムデータ属性にアクセスするには、data()を使う方法と、attr()を使う方法の2つあります。

パフォーマンスはどれがよいのでしょうか?

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="dog" data-dog-breed="poodle">プードルがダルメシアンに変身</div>

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
dataset IDL
var mydog = document.getElementById('dog');
mydog.dataset.dogBreed = 'dalmatian';
pending…
getAttribute() / setAttribute()
var mydog = document.getElementById('dog');
mydog.setAttribute('data-dog-breed','dalmatian');
pending…
data()
$('#dog').data('dogBreed','dalmatian');
pending…
attr()
$('#dog').attr('data-dog-breed','dalmatian');
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