var markup = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"><g class="joint-port"/></svg>';
var list = [],
n = 0;
while(true) {
n++;
list.push(new DOMParser().parseFromString(markup, 'text/xml'));
if(n===1000)
break;
}
var list = [],
n = 0,
node = new DOMParser().parseFromString(markup, 'text/xml');
while(true) {
n++;
list.push(node.cloneNode(true));
if(n===1000)
break;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneNode |
Test name | Executions per second |
---|---|
createElement | 90.9 Ops/sec |
cloneNode | 286.7 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the performance of two approaches: parseFromString
and cloneNode
. The test case uses the DOMParser
library, which parses XML or HTML strings into a DOM document.
Options Compared
The benchmark tests two options:
parseFromString
: This method creates a new DOM document from an XML or HTML string.cloneNode
: This method creates a deep copy of a node in the DOM.Pros and Cons of Each Approach
parseFromString
Pros:
Cons:
cloneNode
Pros:
Cons:
parseFromString
due to the overhead of creating a new DOM documentDOMParser Library
The DOMParser
library is part of the W3C's DOM Standard. Its purpose is to parse XML or HTML strings into a DOM document that can be accessed and manipulated by JavaScript. The library provides methods for parsing different types of documents, such as parseFromString
(for HTML and XML) and parseText
.
Other Considerations
Alternative Approaches
Other approaches for creating new DOM elements could include:
Document.createElement
method to create a new element from scratch.These alternatives may offer different trade-offs in terms of performance, memory usage, and complexity.