Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
getComputedStyle vs matchMedia (javi)
(version: 0)
Comparing performance of:
getComputedStyle vs matchMedia
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="foo"></div> <style> .foo { transition: none; } </style>
Tests:
getComputedStyle
var el = document.querySelector('foo'); var result = window.getComputedStyle().getPropertyValue('transition');
matchMedia
var el = document.querySelector('foo'); var result = window.matchMedia('(prefers-reduced-motion)').matches;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
getComputedStyle
matchMedia
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
getComputedStyle
0.0 Ops/sec
matchMedia
642665.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches for detecting reduced motion in web media: `window.getComputedStyle()` and `window.matchMedia()`. The benchmark creates an HTML element with a class of "foo", sets up a style rule that disables transitions, and then runs two test cases: 1. Using `getComputedStyle()` to retrieve the value of the "transition" property. 2. Using `matchMedia()` to check if the user prefers reduced motion. **Test Case 1: getComputedStyle** The first test case uses the `window.getComputedStyle()` method to access the style properties of an HTML element (`el`). Specifically, it retrieves the value of the "transition" property using the `getPropertyValue()` method. This test case is likely used to measure the overhead of accessing and parsing CSS styles. **Test Case 2: matchMedia** The second test case uses the `window.matchMedia()` API to check if the user prefers reduced motion. It creates a media query string `(prefers-reduced-motion)` and passes it to the `matches()` method, which returns a boolean value indicating whether the condition is met. **Library Used: None** In this benchmark, no external libraries are used. The test cases only rely on the built-in JavaScript APIs. **Special JS Features/Syntax: None** There are no special JavaScript features or syntax used in this benchmark that would require additional explanation beyond what's already covered. **Other Alternatives** If you're looking for alternative approaches to measure performance, consider: 1. **Benchmarking libraries**: Tools like `Benchmark.js`, `jsperf`, or `BenchMark` can simplify the process of creating and running benchmarks. 2. **Web Performance APIs**: Other web performance APIs, such as `requestAnimationFrame()` or `performance.now()`, might provide alternative ways to measure execution time or other performance metrics. 3. **Manual benchmarking**: You can also use manual benchmarking techniques, such as using a loop counter or timing functions like `Date.now()`, but this approach requires more effort and can be less accurate. **Pros and Cons of Different Approaches** The pros and cons of the two approaches being compared in this benchmark are: * **getComputedStyle()** + Pros: - Easy to implement - Direct access to CSS styles + Cons: - May incur additional overhead due to parsing and accessing style properties - Can be slower than other methods for certain use cases (e.g., when dealing with large amounts of data) * **matchMedia()** + Pros: - Efficiently checks media query conditions - Can provide a better indication of the user's preferences + Cons: - May require additional setup and configuration - Can be slower than other methods for certain use cases (e.g., when dealing with simple queries) Keep in mind that the performance difference between these two approaches may vary depending on the specific use case, browser version, and hardware.
Related benchmarks:
client width vs matchmedia
matchMedia vs clientWidth vs innerWidth
clientHeight vs getComputedStyle().height
clientHeight vs getComputedStyle().height no parse float
getComputedStyle vs clientWidth
Comments
Confirm delete:
Do you really want to delete benchmark?