<html>
<body>
<div id='test'>
<div class='item' id='a' name='a'></div>
<div class='item' id='b' name='b'></div>
<div class='item' id='c' name='c'></div>
</div>
</body>
</html>
var parent = document.getElementById('test');
var store1 = [];
var store2 = [];
parent.childNodes.forEach(function (node) {
if ( node.nodeName !== '#text') {
store1.push( node );
store2.push( { name: node.attributes.name.value } );
}
});
let elem = parent.firstElementChild;
do {
let n = elem.attributes.name.value;
} while (elem = elem.nextElementSibling)
let children = parent.getElementsByTagName('div');
for (let i = 0; i < children.length; i++) {
let n = children[i].attributes.name.value;
}
let children = parent.getElementsByClassName('item')
for (let i = 0; i < children.length; i++) {
let n = children[i].attributes.name.value;
}
let n1 = document.getElementById('a').attributes.name.value;
let n2 = document.getElementById('b').attributes.name.value;
let n3 = document.getElementById('c').attributes.name.value;
for (let i = 0; i < store1.length; i++) {
let n = store1[i].attributes.name.value;
}
for (let i = 0; i < store2.length; i++) {
let n = store2[i].name;
}
let elem = parent.firstElementChild;
do {
let n = elem.getAttribute('name');
} while (elem = elem.nextElementSibling)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
fastest iter childelements | |
tag name | |
class name | |
id | |
array of node | |
array item | |
fastest iter childelements + getAttribute |
Test name | Executions per second |
---|---|
fastest iter childelements | 1264905.4 Ops/sec |
tag name | 827169.4 Ops/sec |
class name | 810626.9 Ops/sec |
id | 608968.5 Ops/sec |
array of node | 792838.8 Ops/sec |
array item | 2102137.2 Ops/sec |
fastest iter childelements + getAttribute | 2078095.9 Ops/sec |
Let's break down the benchmark and its components.
Benchmark Overview
The benchmark, named "DOM get attributes of children vs JS Array READ performance", compares the performance of different methods for reading attributes from DOM elements versus JavaScript arrays.
Benchmark Definition JSON
The JSON file defines two main scripts:
Script Preparation Code
: This code sets up a test environment by creating a parent element with child elements, and then loops through each child element to create two arrays: store1
containing the child nodes, and store2
containing objects with attribute names.Html Preparation Code
: This HTML code defines the structure of the test page.Individual Test Cases
The benchmark consists of six individual test cases:
nextElementSibling
and accesses an attribute value.getElementsByTagName
to get a list of child elements with the tag name 'div', then loops through the list to access attribute values.getElementsByClassName
to get a list of child elements with the class name 'item'.getElementById
.store1
array containing the child nodes.store2
array containing objects with attribute names.Library and JavaScript Feature
None of these test cases use specific libraries or advanced JavaScript features like ES6 promises, async/await, or Web Workers.
Performance Comparison
The benchmark measures the performance (in executions per second) of each test case using Chrome 95 on a Linux desktop. The results indicate that reading attribute values from DOM elements is generally faster than accessing them through arrays.
Pros and Cons of Different Approaches
Here are some pros and cons for each approach:
Other Alternatives
For comparing performance in JavaScript, you might also consider using benchmarks like:
Keep in mind that benchmarking is an art, and results may vary depending on your specific use case and environment.