<section id="section"></section>
const section = document.getElementById("section");
section.style.setProperty('--translation', '100');
section.style.setProperty('--translationPx', '100px');
section.style.setProperty('--translationPercentage', '100%');
for (var i = 0; i < 1000; i++) {
const newDiv = document.createElement("div");
const text = document.createTextNode(`${i}`);
newDiv.appendChild(text);
section.appendChild(newDiv);
}
var nodes = document.querySelectorAll("div");
for (var i = 0; i < nodes.length; i++) {
nodes[i].style = "transform: translateY(calc(var(--translation) * 1%))"
}
for (var i = 0; i < nodes.length; i++) {
nodes[i].style = "transform: translateY(var(--translationPercent))"
}
for (var i = 0; i < nodes.length; i++) {
nodes[i].style = "transform: translateY(100%)"
}
for (var i = 0; i < nodes.length; i++) {
nodes[i].style = "transform: translateY(calc(var(--translation) * 1px))"
}
for (var i = 0; i < nodes.length; i++) {
nodes[i].style = "transform: translateY(100px)"
}
for (var i = 0; i < nodes.length; i++) {
nodes[i].style = "transform: translateY(var(--translationPx))"
}
for (var i = 0; i < nodes.length; i++) {
nodes[i].style = "transform: translateY(calc(100% * 1))"
}
for (var i = 0; i < nodes.length; i++) {
nodes[i].style = "transform: translateY(calc(100px * 1))"
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
var(unitless) + calc() + % | |
var(%) | |
Percentage | |
var(unitless) + calc() + px | |
px | |
var(px) | |
%+ calc() | |
px + calc() |
Test name | Executions per second |
---|---|
var(unitless) + calc() + % | 1192.6 Ops/sec |
var(%) | 1301.0 Ops/sec |
Percentage | 1618.1 Ops/sec |
var(unitless) + calc() + px | 1181.9 Ops/sec |
px | 1615.0 Ops/sec |
var(px) | 1337.2 Ops/sec |
px + calc() | 1202.6 Ops/sec |
% + calc() | 1232.4 Ops/sec |
I'll break down the provided benchmark and its options, pros and cons, and other considerations.
Benchmark Overview
The test case measures the performance of different approaches to calculate the translation value for an element's transform property using CSS calc() functions with var() expressions. The tests compare six scenarios:
Options Compared
The options compared are:
var(unitless)
+ calc()
+ %
(unitless value with percentage)var(%)
(percentage value only)Percentage
(absolute value of 100%)var(px)
+ calc()
+ px
(absolute value in px with calculation)px
(absolute value in px only)var(unitless) + calc() + %
and var(unitless) + calc() + px
)Pros and Cons
Each option has its pros and cons:
Other Considerations
var()
expressions introduces a new variable that needs to be looked up and resolved, which may add overhead.calc()
functions requires more CPU cycles than simple arithmetic operations.Best Practice Recommendations
Based on this benchmark, it's recommended to:
calc()
function calculations by reducing the number of operations or using more efficient algorithms.Keep in mind that this is a benchmark specific to Chrome 118, and results may vary across other browsers and environments.