<svg width="150" height="150">
<text id="test1" y="50">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus enim leo, suscipit nec ligula at, pulvinar viverra dolor. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer pulvinar ex vel viverra hendrerit. Sed dui ante, posuere in tempor vitae, pretium eu libero. Nullam ac sem nibh. Vivamus sed est rhoncus augue dignissim dapibus id sit amet arcu. Duis ac faucibus sem. Mauris in nulla urna. Maecenas laoreet lacus magna, vulputate laoreet felis consectetur finibus.</text>
<text id="test2" y="100">
<tspan id="test3">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus enim leo, suscipit nec ligula at, pulvinar viverra dolor. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer pulvinar ex vel viverra hendrerit. Sed dui ante, posuere in tempor vitae, pretium eu libero. Nullam ac sem nibh. Vivamus sed est rhoncus augue dignissim dapibus id sit amet arcu. Duis ac faucibus sem. Mauris in nulla urna. Maecenas laoreet lacus magna, vulputate laoreet felis consectetur finibus.</tspan>
</text>
<tspan id="test4">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus enim leo, suscipit nec ligula at, pulvinar viverra dolor. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer pulvinar ex vel viverra hendrerit. Sed dui ante, posuere in tempor vitae, pretium eu libero. Nullam ac sem nibh. Vivamus sed est rhoncus augue dignissim dapibus id sit amet arcu. Duis ac faucibus sem. Mauris in nulla urna. Maecenas laoreet lacus magna, vulputate laoreet felis consectetur finibus.</tspan>
</svg>
var test1 = document.getElementById('test1');
var test2 = document.getElementById('test2');
var test3 = document.getElementById('test3');
var test4 = document.getElementById('test4');
test1.getBBox().width;
test2.getBBox().width;
test3.getBBox().width;
test4.getBBox().width;
test1.getBoundingClientRect().width;
test2.getBoundingClientRect().width;
test3.getBoundingClientRect().width;
test4.getBoundingClientRect().width;
test1.getComputedTextLength();
test2.getComputedTextLength();
test3.getComputedTextLength();
test4.getComputedTextLength();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getBBox | |
getBoundingClientRect | |
getComputedTextLength |
Test name | Executions per second |
---|---|
getBBox | 100775.5 Ops/sec |
getBoundingClientRect | 87579.1 Ops/sec |
getComputedTextLength | 203426.9 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Overview
The benchmark compares the performance of three different methods to retrieve width information from HTML elements:
getBBox()
: Returns the bounding box of an element, which includes its width, height, and other dimensions.getBoundingClientRect()
: Returns a rectangle object with properties like width
, height
, etc., representing the element's size relative to the viewport.getComputedTextLength()
: Returns the length of the text within an element.Options compared
The benchmark compares the execution times for each method on three different elements:
test1
and test2
: Two <text>
elements with varying amounts of text content, which affects their width values.test3
and test4
: Similar to test1
and test2
, but with a longer text string.Pros and cons of each approach
getBBox()
because it only retrieves the necessary dimensions (width, height) without considering the element's size relative to the viewport.Library usage
The benchmark uses two HTML libraries:
document.getElementById()
: A native JavaScript API for selecting HTML elements based on their IDs.svg
library (not explicitly used, but implied by the presence of <text>
and <tspan>
elements): SVG (Scalable Vector Graphics) is a markup language used to create two-dimensional vector graphics.Special JS features or syntax
The benchmark uses:
Benchmark Definition
strings to concatenate and format values.Alternatives
If you're interested in exploring alternatives, here are a few options:
getComputedStyle()
instead of getBoundingClientRect()
: While getComputedStyle()
can provide similar functionality, it may be slower or more complex due to its need for the CSS styles and layout context.getBBox()
and getComputedTextLength()
, you could write custom JavaScript functions to calculate widths based on element dimensions, padding, and margins.Keep in mind that the performance differences between these approaches may be relatively small, and the benchmark's focus is on comparing the relative speeds of getBBox()
, getBoundingClientRect()
, and getComputedTextLength()
for specific use cases.