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)
})
[document.body.children].forEach(node => {
node.offsetWidth
})
[document.body.children].forEach(node => {
node.getBoundingClientRect().width
})
window.data.forEach(text => {
window.context.measureText(text)
})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
offsetWidth | |
getBoundingClientRect | |
canvas |
Test name | Executions per second |
---|---|
offsetWidth | 3770.9 Ops/sec |
getBoundingClientRect | 1386.9 Ops/sec |
canvas | 26.8 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Definition
The website provides a JSON representation of the benchmark, which includes:
randomString
function to generate random strings.randomString
function.Individual Test Cases
The benchmark consists of three test cases:
[...document.body.children].forEach
method to iterate over all child elements in the document body.offsetWidth
property, which returns the width of the element in pixels.[...document.body.children].forEach
method to iterate over all child elements.getBoundingClientRect().width
property instead of just offsetWidth
.measureText()
method on a string.window.data
) and loops through each string, calling the measureText()
method on the canvas context.Options Compared
The benchmark compares three different approaches:
getBoundingClientRect().width
property instead.measureText()
method on each string.Pros and Cons
Here's a brief analysis of the pros and cons of each approach:
offsetWidth
, with an added benefit of not requiring a full DOM traversal.Library Usage
The benchmark uses a few libraries:
Canvas
: A built-in JavaScript library for working with HTML5 canvas elements.getBoundingClientRect
is a W3C standard property, so no additional library is required.Special JS Feature/Syntax
The benchmark uses the following special feature/syntax:
[...document.body.children]
): Used to iterate over an array of child elements in the document body.randomString()
, measureText()
) are not explicitly used, but the benchmark still measures the execution time of these functions.Other Alternatives
If you need alternatives to the benchmarked approaches:
offsetWidth
or getBoundingClientRect
, consider using CSS layouts like width: fit-content
or display: grid
for more efficient measurements.Keep in mind that the choice of approach ultimately depends on your specific use case and performance requirements.