Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
computedStyleMap vs getComputedStyle (once v2)
(version: 1)
Comparing performance of:
getComputedStyle vs computedStyleMap
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="foo"></div>
Tests:
getComputedStyle
var i = 1; var el = document.querySelector(".foo"); while (i--) { window.getComputedStyle(el).color }
computedStyleMap
var i = 1; var el = document.querySelector(".foo"); while (i--) { el.computedStyleMap().color }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
getComputedStyle
computedStyleMap
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getComputedStyle
1234720.5 Ops/sec
computedStyleMap
7782348.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares two methods for retrieving the computed styles of an HTML element in the browser: `getComputedStyle` and `computedStyleMap`. The goal is to evaluate their performance and to understand the pros and cons of each approach. ### Benchmark Overview The benchmark runs two tests: 1. **`getComputedStyle`**: A traditional method to obtain the computed style of an element. The test executes this method in a loop to measure how many times it can retrieve the color property of an element with the class `.foo`. 2. **`computedStyleMap`**: A newer method that returns a `CSSStyleValueMap` representing all computed CSS properties in a structured format. This also retrieves the color property but does so in a way that allows developers to access more detailed information about individual style properties. ### Performance Results The benchmark results indicate the performance of each method in terms of executions per second: - **`computedStyleMap`**: 12,807,623 executions per second - **`getComputedStyle`**: 1,633,346.125 executions per second The numbers reveal that `computedStyleMap` is significantly faster than `getComputedStyle` in this specific test. ### Pros and Cons of Each Method #### `getComputedStyle` - **Pros**: - Widely supported across all modern browsers and has a long-standing presence in web development. - Straightforward to use for retrieving a specific CSS property value. - **Cons**: - Performance is comparatively lower, especially in cases where frequent style calculations are needed. - Returns a string value for style properties, which may require parsing for complex types or calculations. #### `computedStyleMap` - **Pros**: - Offers better performance as seen in the benchmark results. - Provides a structured representation of CSS properties, allowing easier manipulation and access to specific attributes or values. - Supports new CSS value types directly, enhancing the ability to work with different types of CSS properties. - **Cons**: - May not be as familiar to developers who are used to `getComputedStyle`, requiring some ramp-up time to fully understand and adopt. - Since it's newer, there may be limited support in older browsers or edge cases where fallback methods are necessary. ### Considerations When using either approach, consider the browser compatibility requirements for your project. While most modern browsers support `computedStyleMap`, some older versions or niche browsers may only support `getComputedStyle`. Additionally, while the performance advantage of `computedStyleMap` is notable, profiling the actual performance improvements in the context of your specific application and use case is advised. ### Alternatives In addition to these methods, other alternatives for manipulating or reading computed styles could include: - **CSS Variables**: If a project is heavily reliant on dynamic styles, using CSS custom properties could be an alternative way to manage styles efficiently with JavaScript or CSS. - **Element.style**: If styles are applied inline, accessing elements through the `style` property can provide immediate access to applied styles without needing to use either method. - **CSS-in-JS Libraries**: Libraries like Styled Components or Emotion in React enable the creation of styled components that dynamically handle styles based on props. Overall, the choice between these methods ultimately depends on the performance requirements of your application and the specificities of the styles being manipulated.
Related benchmarks:
window.getComputedStyle vs. className
window.getComputedStyle vs. className vs. className / contains
getComputedStyle vs. className vs. className / contains
window.getComputedStyle vs getAttribute
window.getComputedStyle vs. className2
elem.ownerDocument.defaultView.getComputedStyle vs window.getComputedStyle
window.getComputedStyle vs. className vs. classList 2
computedStyleMap vs getComputedStyle
computedStyleMap vs getComputedStyle (once)
Comments
Confirm delete:
Do you really want to delete benchmark?