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());
if(n===100000)
break;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
deep cloneNode | |
cloneNode |
Test name | Executions per second |
---|---|
createElement | 19.1 Ops/sec |
deep cloneNode | 30.2 Ops/sec |
cloneNode | 28.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is tested on MeasureThat.net?
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks to compare the performance of different approaches in creating new DOM elements.
In this specific benchmark, three test cases are compared:
createElement
: This approach creates a new DOM element using the document.createElement()
method.deep cloneNode
: This approach clones an existing DOM node using the cloneNode(true)
method to create a deep copy of the node.cloneNode
: This approach also clones an existing DOM node, but without the true
parameter in cloneNode()
, which only creates a shallow copy.Options compared
The three test cases compare different approaches to creating new DOM elements:
createElement
: Creates a new element from scratch using document.createElement()
.deep cloneNode
: Clones an existing node and returns a deep copy of it.cloneNode
: Clones an existing node, but only creates a shallow copy.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
deep cloneNode
, as it only creates a shallow copy.Library and purpose
None of these approaches rely on any specific JavaScript library. They are built-in DOM methods that are available in most browsers.
Special JS feature or syntax
There is no special JS feature or syntax used in this benchmark. The focus is solely on comparing the performance of different approaches to creating new DOM elements.
Other alternatives
If you're interested in exploring alternative approaches, here are a few options:
Element.prototype.slice()
to create a new element: This approach creates a new element by slicing an existing one.$().html()
method: This approach clones an existing element and replaces its contents with new ones.Keep in mind that these alternatives may have different performance characteristics compared to the approaches tested on MeasureThat.net.