const test = document.createElement('div')
test.innerHTML = '<div>foo bar</div>'
document.body.append(test.childNodes)
const test = document.createRange().createContextualFragment('<div>foo bar</div>')
document.body.append(test)
const test1 = document.createElement('div')
const test2 = document.createRange().createContextualFragment('<div>foo bar</div>')
test1.append(test2)
document.body.append(test1.childNodes)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DOM | |
DomFragment | |
hybrid |
Test name | Executions per second |
---|---|
DOM | 260839.2 Ops/sec |
DomFragment | 4818.3 Ops/sec |
hybrid | 2662.1 Ops/sec |
Let's break down what is being tested in the provided JSON.
Benchmark Definition
The benchmark definition specifies three different approaches for appending child nodes to a div
element:
innerHTML
property is set directly on the div
element.createRange().createContextualFragment()
method, which creates a new fragment that contains a range of elements. The fragment is then appended to the document body.document.createElement()
, appends another child node (which is created using createRange().createContextualFragment()
) to it, and finally appends the resulting hybrid node to the document body.Options Compared
The three approaches are compared in terms of their performance, specifically:
ExecutionsPerSecond
value)Pros and Cons of Each Approach
createRange().createContextualFragment()
)Library and Purpose
The createRange()
method is part of the W3C Standard (HTML5), and it creates a range object that represents a subset of elements in an HTML document. The createContextualFragment()
method is also part of the W3C Standard (HTML5) and is used to create a new fragment that contains a range of elements.
Special JS Feature or Syntax
There is no specific JavaScript feature or syntax mentioned in this benchmark, but it's worth noting that createRange().createContextualFragment()
is an optimized method that takes advantage of the browser's internal fragment caching mechanism. This optimization can lead to faster execution times compared to creating and manipulating DOM nodes directly.
Other Alternatives
If you're looking for alternative approaches or want to explore different use cases, you could consider:
jsdom
or puppeteer
to simulate browser behavior in Node.jsdocument.createDocumentFragment()
Keep in mind that the specific use case and requirements will influence the choice of approach.