caching jquery data retrieval

JavaScript performance comparison

Test case created by Josh Gruber

Info

see if there's any caching going on

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="id" data-item-one="one" data-item-two="two" data-item-three="three">hello</div>
<script>
Benchmark.prototype.setup = function() {
    var output;
};
</script>

Preparation code output

hello

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
using cached target thrice
var $cachedTarget = $('#id');
output = $cachedTarget.data('itemOne');
output = $cachedTarget.data('itemTwo');
output = $cachedTarget.data('itemThree');
pending…
using object thrice
output = $('#id').data('itemOne');
output = $('#id').data('itemTwo');
output = $('#id').data('itemThree');
pending…
using object once
output = $('#id').data('itemOne');
pending…
using cached once
var $cachedTarget = $('#id');
output = $cachedTarget.data('itemOne');
pending…
using jQuery thrice
$('#id').data();
output = $.data('#id', 'itemOne');
output = $.data('#id', 'itemTwo');
output = $.data('#id', 'itemThree');
pending…
using jQuery once
$('#id').data();
output = $.data('#id', 'itemOne');
pending…
using cached data thrice
var dataCached = $('#id').data();
output = dataCached['itemOne'];
output = dataCached['itemTwo'];
output = dataCached['itemThree'];
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