jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
element.dataset actually sets/gets the attribute. jQuery.data() stores the data internally (and has cross-browser support). jQuery's higher level, chainable .data() method can read data-* attributes but not set them--it stores the data to the selector but doesn't update the DOM.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="test"></div>
function datasetDemo() {
var testElem = document.querySelector('#test'); // native
testElem.dataset.foo = 'bar'; //set
return testElem.dataset.foo; //get
}
function $dataDemo() {// lower level (internal) jQuery method
//var $testElem = $('#test'); // jQuery if needed but...
//var $testElem = document.querySelector('#test'); // native works here
var $testElem = document.getElementById('test'); // native works here
$.data($testElem, 'foo', 'bar'); //set
return $.data($testElem, 'foo'); //get
}
function chain$dataDemo() {// higher level (chainable) jQuery method
var $testElem = $('#test'); // jQuery
$testElem.data('foo', 'bar'); //set
return $testElem.data('foo'); //get
}
function $attrDemo() {// jQuery .attr() method
var $testElem = $('#test'); // jQuery
$testElem.attr('data-foo', 'bar'); //set
return $testElem.attr('data-foo'); //get
}
function nativeAttrDemo() {// getAttribute/setAttribute
var testElem = document.querySelector('#test'); // native
testElem.setAttribute('data-foo', 'bar'); //set
return testElem.getAttribute('data-foo'); //get
}
Ready to run.
Test | Ops/sec | |
---|---|---|
datasetDemo() |
| ready |
$dataDemo() |
| ready |
chainable .data() |
| ready |
$attrDemo() |
| ready |
setAttribute/ getAttribute |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.