Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reading textbox: textContent vs substringData
(version: 0)
Comparing performance of:
data.slice vs substringData vs textContent.slice
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var xs = [...Array(1000).keys()].map(i => { var textbox = document.createElement(`div`); textbox.contentEditable = true; textbox.textContent = [...Array(10000).keys()].map(n => String.fromCharCode(((n+i) % 10) + 65)).join(``); return textbox; }); var t = 0;
Tests:
data.slice
let textbox = xs[(Math.random()*1000)|0]; let i = (Math.random()*9000)|0; let s = textbox.firstChild.data.slice(i, i+100); t += 0|(s.length + s.charCodeAt((Math.random()*100)|0));
substringData
let textbox = xs[(Math.random()*1000)|0]; let i = (Math.random()*9000)|0; let s = textbox.firstChild.substringData(i, 100); t += 0|(s.length + s.charCodeAt((Math.random()*100)|0));
textContent.slice
let textbox = xs[(Math.random()*1000)|0]; let i = (Math.random()*9000)|0; let s = textbox.textContent.slice(i, i+100); t += 0|(s.length + s.charCodeAt((Math.random()*100)|0));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
data.slice
substringData
textContent.slice
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares three approaches to read a substring from a `contentEditable` element: `substringData`, `textContent.slice`, and `data.slice`. The test creates a large text content in a container element and measures the time it takes to access a random character within that content using each approach. **Options Compared** 1. **substringData**: This method is used to manipulate the underlying data of an element's substring. It returns a new string object representing the extracted substring. * Pros: Efficient, as it only copies the necessary characters from the original text content. * Cons: Can be slower than other methods due to the overhead of creating a new string object. 2. **textContent.slice**: This method is used to extract a substring from an element's `textContent`. It returns a new string slice representing the extracted substring. * Pros: Convenient, as it provides a simple and intuitive way to access text content. Can be faster than `substringData` due to caching benefits. * Cons: May require additional memory allocation for the resulting string slice, which can lead to performance issues in rare cases. 3. **data.slice**: This method is used to extract a substring from an element's `data` property (not directly applicable here), but it's mentioned as an alternative in the benchmark code. * Pros: None explicitly mentioned, as this method is not suitable for this specific use case. * Cons: Not applicable or incorrect for this benchmark. **Other Considerations** * **DOM Manipulation**: Creating and manipulating DOM elements can introduce overhead due to garbage collection, reflow, and repaint events. However, in this benchmark, the `contentEditable` element is created only once and reused throughout the test. * **Browser Variability**: The benchmark results are specific to Chrome 88 on Linux, which might not be representative of other browsers or environments. **Library Used** None explicitly mentioned in the provided code snippets. However, it's assumed that the browser and its APIs (e.g., `contentEditable`) provide the necessary functionality for this benchmark. **Special JS Features/Syntax** * **contentEditable**: This feature allows an element to be edited directly by the user. In this benchmark, it's used to create a large text content in a container element. * **`|` Operator**: The bitwise OR operator (`|`) is used as a comment separator (`` `\r\n` ``) and within the `substringData`, `textContent.slice`, and `data.slice` expressions. **Alternatives** Other alternatives for accessing or manipulating text content could include: 1. Using `substring` method on the element's `textContent`: `textbox.textContent.substring(i, i+100)` 2. Creating a new `Text` object: `const text = new Text(textbox.firstChild.data); text.slice(i, i+100)` 3. Using a library or framework that provides optimized string manipulation functions. Keep in mind that these alternatives may not be suitable for this specific use case and might have different performance characteristics.
Related benchmarks:
Text.data vs Text.nodeValue
textContent vs innerText (read)
createTextNode vs textContent vs innerText vs innerHTML (for reading)
CUSTOM substring vs slice
Comments
Confirm delete:
Do you really want to delete benchmark?