var list = [],
n = 0;
while(true) {
n++;
list.push(document.createElement('div'));
if(n===100000)
break;
}
var list = [],
n = 0,
node = document.createElement('div');
while(true) {
n++;
list.push(node.cloneNode(true));
if(n===100000)
break;
}
var list = [],
n = 0,
node = document.createElement('div');
while(true) {
n++;
list.push(node.cloneNode(true));
if(n===100000)
break;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode | |
cloneNode lite |
Test name | Executions per second |
---|---|
createElement | 27.9 Ops/sec |
cloneNode | 46.3 Ops/sec |
cloneNode lite | 45.3 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark measures the performance of three approaches to create new DOM elements: createElement
, cloneNode
, and cloneNode lite
. The goal is to find the fastest way to create new DOM elements before insertion.
Options Compared
createElement
: Creates a new DOM element from scratch using the document.createElement()
method.cloneNode
: Clones an existing DOM node using the node.cloneNode(true)
method, which creates a deep copy of the original node.cloneNode lite
: A variant of cloneNode
that only clones the node's child nodes, without creating a new container element.Pros and Cons
createElement
:cloneNode
:cloneNode lite
:createElement
and cloneNode
, with less overhead than cloning an entire node.Library Used
None, as these test cases do not rely on any external libraries.
Special JS Feature/Syntax
None mentioned explicitly. However, it's worth noting that the cloneNode lite
approach relies on a specific implementation detail of the cloneNode
method, which is only available in modern browsers (e.g., Firefox 89+).
Other Alternatives
If you're interested in exploring other approaches, here are some alternatives:
createElement
and cloneNode
methods by implementing them yourself in a way that's tailored to your needs.Keep in mind that these alternatives may require significant modifications to your codebase and might not provide the same level of performance as the benchmarked approaches.