Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Hallo Welt 123
(version: 0)
Comparing performance of:
new vs normal
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<style> #before:before { content: "a"; display: none; } @media(min-width: 200px) { #body { background: red; content: "b"; } } @media(min-width: 600px) { #body { background: blue; content: "c"; } } @media(min-width: 1000px) { #body { background: yellow; content: "d"; } } </style> <div id="body"> </div>
Tests:
new
const matchMedia1 = window.matchMedia("(min-width: 400px)"); const matchMedia2 = window.matchMedia("(min-width: 700px)"); const matchMedia3 = window.matchMedia("(min-width: 1000px)"); const matchMedia4 = window.matchMedia("(min-width: 1200px)"); matchMedia1.addListener((query) => { if (query.matches) { console.log(true); } }) matchMedia2.addListener((query) => { if (query.matches) { console.log(true); } }) matchMedia3.addListener((query) => { if (query.matches) { console.log(true); } }) matchMedia4.addListener((query) => { if (query.matches) { console.log(true); } })
normal
let conf = { breakpoints: { mobile: { max: 768, min: 0 }, tablet: { min: 768 }, desktop: { min: 1024, }, tabletOnly: { min: 768, max: 1024 }, }, isMobile: false, isTablet: false, isDesktop: false, isTabletOnly: false } window.addEventListener('resize', () => { const width = document.documentElement.clientWidth; conf.isMobile = width < conf.breakpoints.mobile.max conf.isTablet = width >= conf.breakpoints.tablet.min conf.isDesktop = width >= conf.breakpoints.desktop.min conf.isTabletOnly = width >= conf.breakpoints.tabletOnly.min && width < conf.breakpoints.tabletOnly.max });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new
normal
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in this benchmark. **Benchmark Definition** The benchmark definition is a JSON object that contains two test cases: "new" and "normal". The script preparation code is a set of HTML styles that create an empty `#body` element on the page. This code is executed before running each test case. **Test Cases** There are two individual test cases: 1. **"new"**: This test case uses the `window.matchMedia()` API to create four media queries: `(min-width: 400px)`, `(min-width: 700px)`, `(min-width: 1000px)`, and `(min-width: 1200px)`. Each query is added as a listener to match the specified width range. When the query matches, it logs `true` to the console. 2. **"normal"**: This test case defines an object `conf` that contains breakpoints for mobile, tablet, desktop, and tablet-only devices. It also sets up an event listener for the `resize` event on the window object. When the window is resized, it updates the `conf` object based on the current width. **Options Compared** The two test cases are compared in terms of their performance when executing the media queries and updating the `conf` object. The "new" test case creates four separate listeners for each query, while the "normal" test case uses a single event listener to update the `conf` object based on the current width. **Pros and Cons** Here are some pros and cons of each approach: * **"new"**: Pros: + Each query is executed independently, which might be desirable if they have different dependencies or side effects. + It's easy to understand and analyze the code. Cons: + There are four separate listeners, which can lead to more memory usage and slower execution times. + The queries are not optimized together, as they're executed separately. * **"normal"**: Pros: + Only one event listener is created, which reduces memory usage and might improve performance. + The queries are optimized together, as the `conf` object is updated based on the current width. Cons: + It's harder to understand and analyze the code, especially for those unfamiliar with the `conf` object. + There might be side effects or dependencies between the different breakpoints. **Other Considerations** When choosing between these two approaches, consider the following factors: * Memory usage: If memory efficiency is crucial, the "normal" approach might be a better choice due to the single event listener. * Code complexity: If readability and maintainability are more important than performance, the "new" approach might be easier to understand and work with. * Query dependencies: If the queries have different dependencies or side effects, it's better to use separate listeners like in the "new" test case. **Library** There is no explicit library mentioned in this benchmark. However, `window.matchMedia()` is a part of the W3C Web API, and the `conf` object is defined manually. **Special JS Feature or Syntax** No special JavaScript features or syntax are used in these test cases. They only utilize standard JavaScript syntax for defining functions, objects, and event listeners.
Related benchmarks:
matchMedia vs clientWidth
matchMedia vs document.body.clientWidth
matchMedia vs cached matchMedia
Hallo Welt
Comments
Confirm delete:
Do you really want to delete benchmark?