Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Measure Multiple Texts
(version: 1)
Using various methods to measure multiple strings of text.
Comparing performance of:
offsetWidth vs getBoundingClientRect vs canvas
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function randomString() { const charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' const max = Math.max(2, Math.floor(Math.random() * 200)) let randomString = '' for (var i = 0; i < max; i++) { const randomPosition = Math.floor(Math.random() * charSet.length) randomString += charSet.substring(randomPosition, randomPosition + 1) } return randomString } const canvas = document.createElement('canvas') window.context = canvas.getContext('2d') window.data = [...Array(1000).keys()].map(randomString) window.data.forEach((text) => { const div = document.createElement('div') div.innerHTML = text document.body.appendChild(div) })
Tests:
offsetWidth
[...document.body.children].forEach(node => { node.offsetWidth })
getBoundingClientRect
[...document.body.children].forEach(node => { node.getBoundingClientRect().width })
canvas
window.data.forEach(text => { window.context.measureText(text) })
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:
21 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
offsetWidth
530.9 Ops/sec
getBoundingClientRect
457.2 Ops/sec
canvas
1543.7 Ops/sec
Related benchmarks:
Text.data vs Text.nodeValue
Object.create(null) vs {} vs Map() key access (heavy)
Split vs Spread (randomized)
Mappers
Comments
Confirm delete:
Do you really want to delete benchmark?