<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="imba" style="margin: 10px;"></div>
window.template = "<div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div><div style='width: 20px;'></div>";
$('#imba').html(window.template).outerWidth(true);
var imba = document.getElementById('imba');
var m = document.createDocumentFragment();
m.innerHTML += window.template;
imba.appendChild(m)
imba.offsetWidth;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jquery | |
native |
Test name | Executions per second |
---|---|
jquery | 253735.0 Ops/sec |
native | 700076.4 Ops/sec |
Let's break down what's being tested in this benchmark.
Overview
The benchmark compares the performance of two approaches to measure the width of an HTML element: offsetWidth
and outerWidth
. The test is designed to simulate a scenario where the width of an element is being calculated, likely for layout or positioning purposes.
Approaches Compared
There are two individual test cases:
outerWidth()
method to measure the width of the HTML element.document.getElementById('imba')
gets a reference to the element with ID imba
.document.createDocumentFragment()
creates a new document fragment.innerHTML
property is set to the template string, which is defined in the Html Preparation Code
.imba
element.offsetWidth
property of the imba
element is accessed.Pros and Cons
Other Considerations
Library Used
The jQuery library is used in the jQuery Approach
test case. It provides a convenient way to manipulate and query DOM elements, but also adds some overhead due to its internal workings.
Special JS Feature/Syntax
There isn't any special JavaScript feature or syntax being used in this benchmark. The focus is on comparing two approaches to measure an element's width.
Alternatives
If you wanted to modify this benchmark to compare other approaches, you could consider: