Element Creation
JavaScript performance comparison
Info
Compare various methods of creating DOM elements.
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<style>
.hidden { display: none; }
</style>
<div class="hidden" id="container">
</div>
<script>
Benchmark.prototype.setup = function() {
var container = document.getElementById('container'),
_ = {},
$ = {};
'table,tbody,tr,td'.split(',').forEach(function(tag) {
var basis = {};
var f = function(attrs) {
var el = basis[tag].cloneNode(false);
if (attrs) {
Object.keys(attrs).forEach(function(it) {
el.setAttribute(it, attrs[it]);
});
}
for (var i = 1; i < arguments.length; i++) {
el.appendChild(arguments[i]);
}
return el;
};
basis[tag] = document.createElement(tag);
_[tag] = f;
});
_.text = function(content) {
return document.createTextNode(content);
};
'table,tbody,tr,td'.split(',').forEach(function(tag) {
var f = function(attrs) {
var el = arguments.callee.basis.cloneNode(false);
if (attrs) {
Object.keys(attrs).forEach(function(it) {
el.setAttribute(it, attrs[it]);
});
}
for (var i = 1; i < arguments.length; i++) {
el.appendChild(arguments[i]);
}
return el;
};
f.basis = document.createElement(tag);
_[tag] = f;
});
function buildera() {
var t = _.table(undefined);
}
function builderb() {
var t = $.table(undefined);
}
function html() {
container.innerHTML = '<table></table>';
}
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
A |
|
pending… |
B |
|
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 and last updated
- Revision 2: published
- Revision 3: published
- Revision 4: published
- Revision 5: published
- Revision 6: published
- Revision 7: published
- Revision 8: published
- Revision 9: published
- Revision 10: published
- Revision 11: published
- Revision 12: published
- Revision 13: published
- Revision 14: published
- Revision 15: published
- Revision 16: published
- Revision 17: published
- Revision 18: published
- Revision 19: published
- Revision 20: published
0 comments