Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Measure Text Methods 2
(version: 0)
Using various methods to measure a single string of text.
Comparing performance of:
offsetWidth vs getBoundingClientRect vs canvas
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id="my-div">Hello World</div>
Script Preparation code:
const canvas = document.createElement('canvas') window.context = canvas.getContext('2d') window.mydiv = document.createElement('canvas'); window.mydiv.innerHTML = "Hello world";
Tests:
offsetWidth
mydiv.offsetWidth
getBoundingClientRect
mydiv.getBoundingClientRect().width
canvas
window.context.measureText('Hello world')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
offsetWidth
getBoundingClientRect
canvas
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
offsetWidth
78771792.0 Ops/sec
getBoundingClientRect
10222759.0 Ops/sec
canvas
2185558.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data to explain what is tested, compare options, and discuss pros and cons of each approach. **Benchmark Overview** The benchmark measures three different methods for measuring the width of a single string of text: 1. `offsetWidth` (using the `mydiv.offsetWidth` property) 2. `getBoundingClientRect().width` (using the `window.mydiv.getBoundingClientRect()` method) 3. `canvas` (using the `window.context.measureText('Hello world')` method) **Library and Purpose** * None of the test cases rely on a specific JavaScript library. However, it's worth noting that the benchmark uses the HTML5 Canvas API (`window.context`) to measure text width in the third test case. The Canvas API is a 2D drawing API that provides low-level control over rendering graphics on the screen. **JavaScript Features and Syntax** None of the test cases use special JavaScript features or syntax that would be unfamiliar to most software engineers. **Options Compared** The three options are compared in terms of their performance, with the goal of determining which method is the fastest. The benchmark provides raw execution counts per second for each option on a specific device (Mac OS X 10.15.7, Chrome 101). **Pros and Cons of Each Approach** Here's a brief summary: 1. **`offsetWidth`**: * Pros: Simple and widely supported; can be used with many HTML elements. * Cons: May not be accurate for all types of text (e.g., non-rectangular shapes) or fonts (e.g., sans-serif). 2. **`getBoundingClientRect().width`**: * Pros: More accurate than `offsetWidth`, as it measures the bounding rectangle's width, which can account for text layout. * Cons: Requires additional computation and may be slower due to the additional method call. 3. **`canvas` (using `measureText`)**: * Pros: Highly accurate, as it uses the Canvas API's built-in text measurement capabilities. * Cons: May require more setup and context creation compared to other methods. **Other Considerations** When choosing a method for measuring text width, you should also consider factors such as: * Browser compatibility * Text rendering engine (e.g., Blink vs. Gecko) * Font support and rendering quality In this benchmark, the Canvas API method (`canvas`) appears to be the fastest on the specific device tested, but it's essential to evaluate performance in different contexts and browsers. **Alternatives** Other alternatives for measuring text width include: * Using a CSS grid or flexbox layout * Implementing a custom text measurement algorithm (e.g., using SVGs) * Utilizing other APIs like `window.getComputedStyle()` with the `width` property These alternatives might offer better performance, accuracy, or compatibility in specific scenarios, but their implementation and complexity can vary significantly.
Related benchmarks:
Measure Single Text
Measure Text Methods
Measure Text Methods fixed
Measure Text Methods fairer
Comments
Confirm delete:
Do you really want to delete benchmark?