<template id="frag">
<div id="content">
<p>Content</p>
</div>
</template>
$($('#frag').clone().prop('content'));
$($('#frag').prop('content')).clone();
$(document.querySelector('#frag').content).clone();
$(document.querySelector('#frag').cloneNode(true).content);
$(document.querySelector('#frag').content.cloneNode(true));
$(document.getElementById('frag').content.cloneNode(true));
document.querySelector('#frag').cloneNode(true).content;
document.querySelector('#frag').content.cloneNode(true);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery(jQuery.clone()) | |
jQuery(jQuery).clone() | |
jQuery(JavaScript).clone() | |
jQuery(JavaScript.cloneNode.content) | |
jQuery(JavaScript.content.cloneNode) | |
jQuery(getElementById.content.cloneNode) | |
Pure JavaScript.cloneNode.content | |
Pure JavaScript.content.cloneNode |
Test name | Executions per second |
---|---|
jQuery(jQuery.clone()) | 0.0 Ops/sec |
jQuery(jQuery).clone() | 0.0 Ops/sec |
jQuery(JavaScript).clone() | 0.0 Ops/sec |
jQuery(JavaScript.cloneNode.content) | 0.0 Ops/sec |
jQuery(JavaScript.content.cloneNode) | 0.0 Ops/sec |
jQuery(getElementById.content.cloneNode) | 0.0 Ops/sec |
Pure JavaScript.cloneNode.content | 683143.2 Ops/sec |
Pure JavaScript.content.cloneNode | 826647.1 Ops/sec |
Measuring the performance of JavaScript code can be a complex task, and MeasuredThat.net provides an interesting benchmark to compare different approaches.
The provided JSON represents a benchmark that tests various styles of retrieving the content of a template
element using jQuery and pure JavaScript. There are eight individual test cases:
Options Compared:
$($('#frag').clone().prop('content'));
.$($('#frag').prop('content')).clone();
.div
element within the template
using $(document.querySelector('#frag').content).clone();.template
node (including its contents) and then extracting it using $(document.querySelector('#frag').cloneNode(true).content);document.querySelector('#frag').cloneNode(true).content;
.template
node (including its contents) and then extracting it using document.querySelector('#frag').content.cloneNode(true);
.Pros and Cons of Each Approach:
prop('content')
, which might incur additional overhead.clone()
. This could be slower due to the additional function call.querySelector
to select the content element, which can be slower than using a direct selector like #frag
.Other Considerations:
template
element is used here, which is a HTML5 feature. If you're targeting older browsers, you may need to use alternative approaches.Alternative Approaches:
innerHTML
: Instead of cloning the node and extracting the content, you can use innerHTML
to set the HTML content of a new element. This approach is generally faster than cloning, but it assumes you have control over the resulting HTML structure.substring()
or slice()
: If you know that the content is contained within a specific range (e.g., between 0
and length - 1
), you can use substring()
or slice()
to extract the substring. This approach can be faster than cloning, but it requires knowledge of the content bounds.html()
method or the DOMParser
API to manipulate and extract content.Keep in mind that these alternative approaches may have their own trade-offs and limitations. It's essential to evaluate the performance characteristics of each approach based on your specific use case.