<div id="container"></div>
var container = document.getElementById("container");
var shortString = "Lorem ipsum dolor sit amet.";
var longString = "Curabitur eros quam, pellentesque a tellus vestibulum, imperdiet condimentum nibh. Nullam sollicitudin porta nunc quis feugiat. Pellentesque tincidunt commodo dolor a ullamcorper. Etiam accumsan mauris ac ex interdum ornare. Sed sodales sem sit amet risus viverra efficitur. Phasellus eget leo et mauris volutpat congue. Nam nisl sapien, scelerisque vel pulvinar quis, molestie luctus ex. Integer vel dolor hendrerit, lobortis nisi ac, laoreet velit. Duis luctus sodales pulvinar.";
container.innerHTML = shortString;
container.innerHTML = longString;
container.textContent = shortString;
container.textContent = longString;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerHTML shortString | |
innerHTML longString | |
textContent shortString | |
textContent longString |
Test name | Executions per second |
---|---|
innerHTML shortString | 363047.0 Ops/sec |
innerHTML longString | 177652.0 Ops/sec |
textContent shortString | 369765.2 Ops/sec |
textContent longString | 287717.0 Ops/sec |
Let's dive into the benchmark and explore what's being tested, the options compared, pros and cons of each approach, and other considerations.
Benchmark Overview
The benchmark measures the performance of two text content replacement methods in JavaScript: innerHTML
and textContent
. The test case consists of replacing a short string ("Lorem ipsum dolor sit amet."
) with a long string ("Curabitur eros quam, pellentesque a tellus vestibulum, imperdiet condimentum nibh. Nullam sollicitudin porta nunc quis feugiat. Pellentesque tincidunt commodo dolor a ullamcorper. Etiam accumsan mauris ac ex interdum ornare. Sed sodales sem sit amet risus viverra efficitur. Phasellus eget leo et mauris volutpat congue. Nam nisl sapien, scelerisque vel pulvinar quis, molestie luctus ex. Integer vel dolor hendrerit, lobortis nisi ac, laoreet velit. Duis luctus sodales pulvinar."
).
Benchmark Preparation Code
The script preparation code is:
var container = document.getElementById("container");
var shortString = "Lorem ipsum dolor sit amet.";
var longString = "Curabitur eros quam, pellentesque a tellus vestibulum, imperdiet condimentum nibh. Nullam sollicitudin porta nunc quis feugiat. Pellentesque tincidunt commodo dolor a ullamcorper. Etiam accumsan mauris ac ex interdum ornare. Sed sodales sem sit amet risus viverra efficitur. Phasellus eget leo et mauris volutpat congue. Nam nisl sapien, scelerisque vel pulvinar quis, molestie luctus ex. Integer vel dolor hendrerit, lobortis nisi ac, laoreet velit. Duis luctus sodales pulvinar.";
The HTML preparation code is:
<div id="container"></div>
Individual Test Cases
There are four test cases:
innerHTML shortString
innerHTML longString
textContent shortString
textContent longString
Each test case consists of replacing the text content of an HTML container element with either a short string or a long string using either innerHTML
or textContent
.
Options Compared
The benchmark compares the performance of two replacement methods:
innerHTML
: Replaces the HTML content of an element.textContent
: Replaces the text content of an element.Pros and Cons of Each Approach
innerHTML
Pros:
Cons:
textContent
Pros:
innerHTML
Cons:
Other Considerations
When using innerHTML
, it's essential to use a sandboxed environment (e.g., document.body.innerHTML
) to prevent cross-site scripting (XSS) attacks. In contrast, textContent
is generally safer to use in most scenarios.
Alternative Methods
Besides innerHTML
and textContent
, there are other methods for replacing text content:
replaceChild()
: Replaces the child element with a new one.setInnerHTML()
(not widely supported): Replaces the HTML content of an element.string.prototype.replace()
or regular expressions to manipulate the text content.However, these alternatives are not commonly used and may incur additional overhead compared to innerHTML
and textContent
.
Library
There is no library mentioned in the benchmark definition or test cases.