var textNode = document.createTextNode('');
var div = document.createElement('div');
for (var c=0;c<1000;c++) {
textNode.data = c%2;
}
for (var c=0;c<1000;c++) {
div.setAttribute('tick', c%2);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
TextNode data | |
Div setAttribute |
Test name | Executions per second |
---|---|
TextNode data | 14243.8 Ops/sec |
Div setAttribute | 5969.2 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Overview
The benchmark is designed to compare the performance of two approaches:
Text
node (TextNode data
)Div setAttribute
)Comparison Options
The benchmark tests two options for setting properties and attributes:
data
property to store a value, which is then updated in each iteration of the loop. The data
property is a special property that can be used to store arbitrary data on a Text
node.setAttribute()
method to set an attribute named "tick" on an HTML element (div
). The value of this attribute is updated in each iteration of the loop.Pros and Cons
TextNode data:
Pros:
data
property is stored directly on the node.Cons:
Div setAttribute:
Pros:
Cons:
setAttribute()
method.Library and Special JS Features
In this benchmark, no libraries are used beyond the standard JavaScript API. No special JavaScript features or syntax are applied.
Alternatives
Other alternatives for setting properties and attributes include:
Text
nodes.Benchmark Preparation Code
The provided preparation code is:
var textNode = document.createTextNode('');
var div = document.createElement('div');
This creates an empty Text
node and an empty HTML element (div
). The TextNode data
test case uses these elements to store and update values, while the Div setAttribute
test case uses the same elements but sets attributes instead.