var list = [],
n = 0;
while(true) {
n++;
const div = document.createElement('div');
div.innerHTML = '<span style="display: block; color: red;">hey you</span>'
list.push();
if(n===100000)
break;
}
var list = [],
n = 0,
node = document.createElement('div');
node.innerHTML = '<span style="display: block; color: red;">hey you</span>'
while(true) {
n++;
list.push(node.cloneNode(true));
if(n===100000)
break;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode |
Test name | Executions per second |
---|---|
createElement | 0.3 Ops/sec |
cloneNode | 3.5 Ops/sec |
Let's break down the benchmark and explain what is being tested.
Benchmark Definition
The benchmark is comparing two approaches to create new DOM elements:
document.createElement('div')
node.cloneNode(true)
What is being tested?
In this benchmark, we're testing which approach is faster when creating a large number of DOM elements with some content. The test cases are identical in terms of the content (<span style="display: block; color: red;">hey you</span>
), but differ in how they create new elements.
Options being compared
We have two options:
document.createElement('div')
: This approach creates a new DOM element from scratch, using the createElement()
method.node.cloneNode(true)
: This approach creates a copy of an existing DOM element, using the cloneNode()
method.Pros and Cons
document.createElement('div')
:node.cloneNode(true)
:Library usage
In this benchmark, we're using the document
object and the createElement()
and cloneNode()
methods, which are part of the Web APIs.
Special JS features or syntax
There doesn't appear to be any special JavaScript features or syntax being used in this benchmark. The code is straightforward and relies on standard DOM API methods.
Other alternatives
If you wanted to test alternative approaches, here are a few possibilities:
DOMPurify
or jsdom
to create DOM elements.Keep in mind that these alternatives may have their own trade-offs and performance implications, so it's essential to evaluate them carefully for your specific use case.