<div style='display: none'>
<div id='node'>
<name>cat</name>
<not>blank</not>
<not1>blank</not1>
<not2>blank</not2>
<not3>blank</not3>
<not4>blank</not4>
<type>bat</type>
</div>
<div id='attr' name='cat' type='bat' not='blank' not1='blank' not2='blank' not3='blank' not4='blank'></div>
<div id='node2'><data>[name]cat[not]blank[not1]blank[not2]blank[not3]blank[not4]blank[type]bat[</data></div>
</div>
var node = document.getElementById('node')
var attr = document.getElementById('attr')
var node2 = document.getElementById('node2')
var obj = { name:'cat', not:'blank', not1:'blank', not2:'blank', not3:'blank', not4:'blank', type:'bat' }
var al = {
name:0,
not:1,
not1:2,
not2:3,
not3:4,
not4:5,
type:6
}
let elem = node.firstElementChild;
let n;
do {
if ( elem.nodeName === 'TYPE' ) {
elem.nodeValue='rat';
break;
}
} while (elem = elem.nextElementSibling)
attr.getAttributeNode('type').value='rat';
let t = node2.firstElementChild.firstChild.nodeValue;
let s = t.indexOf('[type]')+6
let e = t.indexOf('[', s)
let n = t.substring(0,s) + 'rat' + t.substring(e)
node.children[al['type']].nodeValue='rat';
let elem = node.firstElementChild;
let n;
for (let i = 0; i<10; i++) {
if ( i === al['type'] ) {
elem.nodeValue = 'rat';
break;
}
elem = elem.nextElementSibling
}
node.children[6].nodeValue='rat';
let elem = node.firstElementChild;
let n;
for (let i = 0; i<10; i++) {
if ( i === 6 ) {
elem.nodeValue = 'rat';
break;
}
elem = elem.nextElementSibling
}
obj['type']='rat';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
write nodes with 7 child nodes | |
write attribute with 7 attributes | |
write nodevalue with 7 string attributes | |
write nodes with 7 child nodes with children + index | |
write nodes with 7 child nodes with nextElementSibling + index | |
write nodes with 7 child nodes with children + no index | |
write nodes with 7 child nodes with nextElementSibling + no index | |
obj with 7 items |
Test name | Executions per second |
---|---|
write nodes with 7 child nodes | 1910044.2 Ops/sec |
write attribute with 7 attributes | 1773468.2 Ops/sec |
write nodevalue with 7 string attributes | 5243155.5 Ops/sec |
write nodes with 7 child nodes with children + index | 3029336.2 Ops/sec |
write nodes with 7 child nodes with nextElementSibling + index | 1349164.1 Ops/sec |
write nodes with 7 child nodes with children + no index | 3970345.2 Ops/sec |
write nodes with 7 child nodes with nextElementSibling + no index | 4039953.5 Ops/sec |
obj with 7 items | 14914012.0 Ops/sec |
Measuring performance differences between various approaches to store data in DOM elements is crucial, especially when considering different use cases like writing attributes, node values, or using objects.
Benchmark Definition
The benchmark tests the execution speed of six different methods for writing data into DOM elements:
nodeValue
properties directly on the element nodes.The benchmark results are shown in the provided "Latest benchmark result" section, which lists execution speeds for each test case across multiple browsers and devices.
Key Takeaways
Why Does It Matter?
Understanding these performance differences is crucial for optimizing your code, especially when dealing with complex data structures or large datasets. Choosing the most efficient approach can significantly impact application performance, user experience, and resource usage.
Best practices like:
Conclusion
Measuring performance differences between various DOM data storage approaches can help you optimize your code, improve application performance, and reduce resource usage. By understanding these key takeaways, you'll be better equipped to choose the most efficient method for your specific use case.