<div id="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur dictum magna dolor, a convallis erat convallis a. Etiam interdum tincidunt leo non elementum. Mauris rhoncus lorem eu dapibus porta. Integer tincidunt mi a justo dignissim suscipit. Aenean accumsan eget eros eget suscipit. Aliquam maximus rhoncus felis, nec vestibulum turpis fermentum a. Donec sagittis augue eros, non vehicula urna mollis volutpat. Pellentesque gravida placerat arcu, et gravida tellus pellentesque sed. Ut efficitur ornare massa nec aliquet. Aenean pharetra elementum aliquet. Etiam faucibus, metus vel consequat lobortis, ligula ipsum hendrerit erat, id dictum justo purus in metus. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
var el = document.getElementById('content');
var replacementText = "Narwhal gentrify squid franzen vape 90's man bun, literally godard raw denim pabst mlkshk. Banjo church-key seitan bushwick, cardigan pop-up single-origin coffee tumblr godard disrupt roof party lyft shoreditch yuccie. Church-key hot chicken umami kitsch, vaporware cardigan pop-up. Waistcoat organic sriracha, hashtag meh single-origin coffee brunch wayfarers small batch mustache. Normcore skateboard mumblecore, cold-pressed small batch live-edge try-hard typewriter waistcoat master cleanse heirloom. Heirloom VHS chia, flannel put a bird on it sustainable portland try-hard 90's locavore. Try-hard lo-fi heirloom, sartorial cray chillwave ennui lyft taxidermy farm-to-table.";
var text = $(el).text();
var text = el.textContent;
$(el).text(replacementText);
el.textContent = replacementText;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
get jQuery.text() | |
get Element.textContent | |
set jQuery.text() | |
set Element.textContent |
Test name | Executions per second |
---|---|
get jQuery.text() | 0.0 Ops/sec |
get Element.textContent | 944017.2 Ops/sec |
set jQuery.text() | 0.0 Ops/sec |
set Element.textContent | 4929951.0 Ops/sec |
Let's break down what is being tested on MeasureThat.net.
Benchmark Definition
The benchmark compares two approaches to set and get text content of an HTML element:
$(el).text()
(jQuery)el.textContent
(JavaScript native)Options Compared
text()
method vs JavaScript's native textContent
property.text()
or textContent
) vs getting text content.Pros and Cons of Each Approach:
text()
Method:textContent
due to additional overhead.textContent
Property:Library: jQuery
jQuery is a popular JavaScript library that simplifies HTML and DOM manipulation. Its text()
method is used extensively in web development for setting and updating text content.
Special JS Feature/Syntax: None Mentioned
This benchmark focuses on the comparison of two specific methods, without introducing any special JavaScript features or syntax.
Other Alternatives
For setting text content, other alternatives to jQuery's text()
method include:
document.getElementById('content').innerHTML = 'newText';
: Using innerHTML
to set text and HTML content.el.setAttribute('innerText', 'newText');
: Using setAttribute
to set the innerText
attribute (available in some browsers).For getting text content, other alternatives to JavaScript's textContent
property include:
el.innerHTML
: Returns the innerHTML of an element, including text content.element.value
: Used for input elements to get or set their value.Keep in mind that these alternatives may have different performance characteristics and use cases compared to jQuery's text()
method or native textContent
property.