Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Create text node
(version: 1)
Comparing performance of:
new Text vs createTextNode vs new Text 2 vs createTextNode 2
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var x=Date.now();
Tests:
new Text
new Text(x)
createTextNode
document.createTextNode(x)
new Text 2
new Text(x)
createTextNode 2
document.createTextNode(x)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
new Text
createTextNode
new Text 2
createTextNode 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:136.0) Gecko/20100101 Firefox/136.0
Browser/OS:
Firefox 136 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new Text
4245326.0 Ops/sec
createTextNode
3176054.8 Ops/sec
new Text 2
4190237.8 Ops/sec
createTextNode 2
3164510.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark tests the performance of different methods of creating text nodes in a web document using JavaScript. It specifically compares two primary approaches: 1. **Using the `Text` constructor**: - `new Text(x)`: This method creates a new `Text` node directly using the `Text` constructor, which is part of the DOM (Document Object Model). It takes a string as an argument and represents a text content node that can be added to the document. - The test includes this method twice, named "new Text" and "new Text 2", but effectively they are testing the same approach in different executions. 2. **Using the `document.createTextNode` method**: - `document.createTextNode(x)`: This method is a more traditional way of creating a text node in the DOM. It also takes a string argument and produces a text node object. It has been available since early versions of the DOM and is widely used in web development. ### Pros and Cons #### Pros of `new Text`: - **Simplicity**: The syntax is straightforward and may lead to cleaner code. - **Consistency**: It is part of the modern JavaScript class-based structure and fits well with other new JavaScript paradigms. #### Cons of `new Text`: - **Compatibility**: While modern browsers support the `Text` constructor, older browsers may not, which could affect the backward compatibility of applications that are not strictly modern. - **Overhead**: Depending on the implementation, some optimizations may be better for `document.createTextNode`. #### Pros of `document.createTextNode`: - **Widespread Use**: This method is familiar to many developers and is well-documented. - **Backward Compatibility**: It is supported in all browsers that implement the DOM, making it a safer choice for supporting legacy systems. #### Cons of `document.createTextNode`: - **Verbosity**: This method can be considered more verbose compared to using the `Text` constructor, leading to slightly less readable code. ### Performance Results The benchmark results show the number of executions per second for each test case, indicating the performance of each approach in creating text nodes. Here are the results: - **`new Text`: 4,245,326 executions/second** - **`new Text 2`: 4,190,237.75 executions/second** - **`createTextNode`: 3,176,054.75 executions/second** - **`createTextNode 2`: 3,164,510 executions/second** From the results, it is evident that using `new Text` is significantly faster than using `document.createTextNode`, demonstrating a potential performance benefit when using the `Text` constructor. ### Other Considerations When deciding between these two methods, consider the browser support required for your application. If you are targeting modern browsers exclusively, using `new Text` could be beneficial due to its higher performance. However, if you need to support older browsers or maintain compatibility across diverse environments, `document.createTextNode` may be the better option. Additionally, depending on the context (e.g., creating a large number of text nodes in a loop), the performance differences could become more pronounced or have more significant implications in terms of application responsiveness and user experience. Always profile performance in the context of your specific application for the best outcomes.
Related benchmarks:
createTextNode, textContent, innerText, innerHTML, nodeValue
checking if nodeValue is equal vs simply setting it
let createTextNode vs textContent vs innerText vs innerHTML
textContent, innerText, innerHTML, nodeValue get value perf
createTextNode, textContent, innerContent, innerText, innerHTML, nodeValue
textContent, nodeValue set value perf
benchmark kk
createTextNode vs nodeValue
Create text node v2
Comments
Confirm delete:
Do you really want to delete benchmark?