var x=Date.now();
var y=''+Math.random();
var z=null
new Text(x)
document.createTextNode(x)
new Text(y)
document.createTextNode(y)
new Text(y)
document.createTextNode(y)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new Text number | |
createTextNode number | |
new Text string | |
createTextNode string | |
new Text null | |
createTextNode null |
Test name | Executions per second |
---|---|
new Text number | 6657346.5 Ops/sec |
createTextNode number | 9134806.0 Ops/sec |
new Text string | 9347326.0 Ops/sec |
createTextNode string | 12851516.0 Ops/sec |
new Text null | 9677646.0 Ops/sec |
createTextNode null | 13262452.0 Ops/sec |
The benchmark presented assesses the performance of two methods for creating text nodes in the browser's Document Object Model (DOM): new Text()
and document.createTextNode()
. These approaches are compared across three different data types for the text content: a number, a string, and null.
new Text(x)
vs document.createTextNode(x)
:Text
node directly using the Text constructor. The main advantage is its modern syntax, which may lead to more readable code.Test Cases:
new Text(x)
where x
is a numberdocument.createTextNode(x)
where x
is a numbernew Text(y)
where y
is a stringdocument.createTextNode(y)
where y
is a stringnew Text(y)
where y
is nulldocument.createTextNode(y)
where y
is nullThe benchmark results show execution rates measured in "Executions Per Second" (EPS) for each test. Here are the results from the latest benchmark run:
createTextNode(null)
: 13,262,452 EPScreateTextNode(string)
: 12,851,516 EPSnew Text(null)
: 9,677,646 EPSnew Text(string)
: 9,347,326 EPScreateTextNode(number)
: 9,134,806 EPSnew Text(number)
: 6,657,346.5 EPSPerformance Pros/Cons:
document.createTextNode()
generally outperforms new Text()
in this benchmark. This might suggest that the traditional DOM method is more optimized in this scenario, possibly due to better integration with the existing DOM manipulations and browser implementations.new Text()
is a newer and more modern JavaScript feature, but its performance in certain scenarios might lag behind the classic methods, as shown in the benchmark results.new Text()
can result in more readable code, as it cleanly expresses the intention of creating a text node. In contrast, document.createTextNode()
may feel outdated, though it maintains broad compatibility.Null Handling:
null
gracefully, resulting in empty text nodes. However, their performance still differs; createTextNode(null)
performed the best overall.String and Number Handling:
Alternative methods within JavaScript for managing text and DOM manipulation include:
innerHTML
to create text nodes, but this approach has performance drawbacks due to DOM reflows and parsing.The benchmark reveals nuanced performance characteristics of two methods for creating text nodes in the DOM. While document.createTextNode()
consistently shows superior performance across tests, modern practices encourage the use of new Text()
for improved code readability. Developers should assess both performance implications and coding practices when deciding on which technique to utilize in production environments, especially considering potential browser-specific optimizations and support.