jQuery().fn.attr vs jQuery().fn.prop vs jQuery().fn.data vs jQuery().data
JavaScript performance comparison
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="el"></div>
<script>
// 'el-lo
var $el = $('#el');
var el = $el[0];
var get; // access holder, so that dead stores aren't so easily culled off.
// Store only 19 strings to test.
// In real code, there usually are not very many
// unique keys that get accessed and mutated.
// It needs to be odd so that after counter%limit loops,
// the attr/prop/data accessors return values.
// This also shakes up the key/value pairs.
var counter, limit = 19;
var rnd = [];
// Pre-create all strings unique, just to make sure nothing funny happens.
for (var i = 0; i < limit; i++) {
rnd[i] = 'X-' + i + '-' + Math.floor(Math.random() * 1001);
}
function nextKey() { return rnd[counter++ % limit]; }
function nextVal() { return rnd[counter++ % limit]; }
</script>
<script>
Benchmark.prototype.setup = function() {
counter = 0;
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
jQuery.fn.attr |
|
pending… |
jQuery.fn.prop |
|
pending… |
jQuery.fn.data |
|
pending… |
jQuery.data(el) |
|
pending… |
jQuery.data($el) |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by Sam Korn and last updated
- Revision 4: published
- Revision 5: published
- Revision 6: published by dmig
- Revision 7: published
- Revision 8: published by amenthes
- Revision 10: published by Mike
- Revision 11: published
- Revision 12: published by SMR
- Revision 13: published
- Revision 14: published
- Revision 15: published by dan
- Revision 16: published by dan
- Revision 17: published
- Revision 18: published
- Revision 19: published
- Revision 20: published
- Revision 21: published
- Revision 22: published
- Revision 24: published
- Revision 26: published by Kenneth Kalmer
- Revision 27: published by Nav
- Revision 28: published by Nav
- Revision 29: published by Mike Frey
- Revision 30: published by devu
- Revision 31: published by Mike Frey
- Revision 32: published
- Revision 33: published by Dmitry
- Revision 35: published
- Revision 36: published
- Revision 37: published
- Revision 38: published
- Revision 39: published
- Revision 42: published
- Revision 43: published
- Revision 44: published by dd
- Revision 47: published by Alexander Schmitz
- Revision 48: published by Alexander Schmitz
- Revision 49: published by Dennis Kim
- Revision 50: published
- Revision 51: published
- Revision 52: published
- Revision 53: published
- Revision 55: published
- Revision 56: published
- Revision 57: published
- Revision 58: published by rj
0 comments