<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/here-is-how-to-write-better-frontend-code-b2k4imgfa" -->

---
title: here is how to write BETTER frontend code | daily.dev
description: A YouTube-style rundown covers five lesser-known built-in browser features that can replace common JavaScript workarounds: requestIdleCallback for scheduling...
canonical: https://daily.dev/posts/here-is-how-to-write-better-frontend-code-b2k4imgfa
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: here is how to write BETTER frontend code | daily.dev
og:description: A YouTube-style rundown covers five lesser-known built-in browser features that can replace common JavaScript workarounds: requestIdleCallback for scheduling...
og:url: https://daily.dev/posts/here-is-how-to-write-better-frontend-code-b2k4imgfa
og:image: https://api.daily.dev/og/posts/B2K4Imgfa.png
og:image:alt: here is how to write BETTER frontend code
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# here is how to write BETTER frontend code

**[CodeHead](https://daily.dev/sources/codehead)** · 4 min read · 82 upvotes · 9 comments

## Summary

A YouTube-style rundown covers five lesser-known built-in browser features that can replace common JavaScript workarounds: requestIdleCallback for scheduling background work without blocking the main thread, the CSS :has() selector for parent-based styling instead of JS event listeners, structuredClone for true deep cloning (including dates and Maps) instead of JSON.parse/stringify, IntersectionObserver for efficient lazy loading instead of scroll event handlers, and container queries for component-level responsive design instead of media queries. The piece ends with a sponsored plugin for Scrimba's new 'Explain' text-to-video tool.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.youtube.com/watch?v=df2H7reofa8>

## Questions this post answers

### How do I clone a JavaScript object that contains a Date or a Map without losing type information?

Use structuredClone instead of JSON.parse(JSON.stringify(obj)). The JSON round-trip approach converts dates to strings and Maps to empty objects, silently corrupting data. structuredClone is a built-in browser deep-cloning algorithm, the same one browsers use to transfer data between tabs, and it preserves dates as dates and Maps as Maps.

_daily.dev surfaces practical browser API tips like this for developers cleaning up cloning logic._

### How can I lazy load images without a scroll event handler that calls getBoundingClientRect repeatedly?

Use the IntersectionObserver API instead of a scroll listener. The old approach recalculates each element's position with getBoundingClientRect on every scroll event, which can fire dozens of times per second, whereas IntersectionObserver registers each element once and lets the browser report visibility changes, cutting a two-second scroll from over 100 calls down to almost none.

_developers optimizing scroll-heavy pages often track browser API techniques like this on daily.dev._

### How do I style a card component differently based on the width of its container instead of the browser window?

Use CSS container queries by setting container-type: inline-size on the parent and applying styles with min-width conditions like min-width: 350px. Unlike media queries, which only respond to viewport width, container queries let the same reusable card component adapt its layout depending on whether it sits in a wide main column or a narrow sidebar.

_daily.dev helps frontend developers keep up with CSS techniques like container queries for reusable components._

## Community discussion

Top comments from developers on daily.dev.

**@confidentcoding** · 7 upvotes

> Legitimate great tips and so refreshing to not see just AI rants. Bravo!

**@ristotoldsep** · 3 upvotes

> Every time `:has()` deletes a JS workaround, I smile a little.

**@mortybingo** · 1 upvotes

> Why's there a hot babe in the thumbnail?

## Similar posts on daily.dev

- [Stop Using JS for Everything: Harnessing the Power of Pure CSS in 2026](https://daily.dev/posts/stop-using-js-for-everything-harnessing-the-power-of-pure-css-in-2026-jql9shws1) · Ruby Flow · 6 upvotes · 0 comments
- [Designing Stable Interfaces For Streaming Content — Smashing Magazine](https://daily.dev/posts/designing-stable-interfaces-for-streaming-content-smashing-magazine-urvigomyr) · Smashing Magazine · 1 upvotes · 0 comments
- [The Frontend Engineering Trick That Makes YouTube Feel Instant](https://daily.dev/posts/the-frontend-engineering-trick-that-makes-youtube-feel-instant-9gmngdpnm) · Medium · 8 upvotes · 1 comments
- [Stop using JavaScript to solve CSS problems](https://daily.dev/posts/stop-using-javascript-to-solve-css-problems-utawse2td) · LogRocket · 208 upvotes · 8 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#performance](https://daily.dev/tags/performance), [#css](https://daily.dev/tags/css), [#ui-design](https://daily.dev/tags/ui-design)

[View this post on daily.dev](https://daily.dev/posts/here-is-how-to-write-better-frontend-code-b2k4imgfa)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"here is how to write BETTER frontend code","url":"https://daily.dev/posts/here-is-how-to-write-better-frontend-code-b2k4imgfa","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/here-is-how-to-write-better-frontend-code-b2k4imgfa"},"datePublished":"2026-09-23T14:01:23.293Z","dateModified":"2026-09-23T14:01:48.336Z","description":"A YouTube-style rundown covers five lesser-known built-in browser features that can replace common JavaScript workarounds: requestIdleCallback for scheduling...","image":"https://i.ytimg.com/vi/df2H7reofa8/sddefault.jpg","thumbnailUrl":"https://i.ytimg.com/vi/df2H7reofa8/sddefault.jpg","isAccessibleForFree":true,"articleSection":"CodeHead","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"CodeHead","logo":"https://media.daily.dev/image/upload/s--ywzrIEkA--/f_auto/v1745508579/logos/codehead","url":"https://daily.dev/sources/codehead"},"commentCount":9,"discussionUrl":"https://daily.dev/posts/here-is-how-to-write-better-frontend-code-b2k4imgfa","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":82},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":9}],"keywords":"javascript,performance,css,ui-design","timeRequired":"PT4M","video":{"@type":"VideoObject","name":"here is how to write BETTER frontend code","description":"A YouTube-style rundown covers five lesser-known built-in browser features that can replace common JavaScript workarounds: requestIdleCallback for scheduling...","thumbnailUrl":"https://i.ytimg.com/vi/df2H7reofa8/sddefault.jpg","uploadDate":"2026-09-23T14:01:23.293Z","duration":"PT4M","url":"https://api.daily.dev/r/B2K4Imgfa","embedUrl":"https://www.youtube.com/embed/df2H7reofa8"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"CodeHead","item":"https://daily.dev/sources/codehead"},{"@type":"ListItem","position":3,"name":"here is how to write BETTER frontend code"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/here-is-how-to-write-better-frontend-code-b2k4imgfa","comment":[{"@type":"Comment","text":"Legitimate great tips and so refreshing to not see just AI rants. Bravo!","datePublished":"2026-09-23T19:40:04.611Z","url":"https://daily.dev/posts/B2K4Imgfa#c-k6WVPnXMm","author":{"@type":"Person","name":"Lars Faye | Confident Coding","url":"https://daily.dev/confidentcoding","image":"https://media.daily.dev/image/upload/s--OGZu5DEc--/f_auto/v1772569630/avatars/avatar_umWZ9aQAng34qk5aaJl2q?_a=BAMAMiiu0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":7}},{"@type":"Comment","text":"Every time :has() deletes a JS workaround, I smile a little.","datePublished":"2026-09-25T08:16:16.660Z","url":"https://daily.dev/posts/B2K4Imgfa#c-HDQMbgYqe","author":{"@type":"Person","name":"Risto Tõldsep","url":"https://daily.dev/ristotoldsep","image":"https://lh3.googleusercontent.com/a/ACg8ocLDWc6mZn0JwNmXXw6WY0L_HJ6pegzRttooC5VgtXESHSj3MNYx=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3}},{"@type":"Comment","text":"Why’s there a hot babe in the thumbnail?","datePublished":"2026-09-24T16:14:15.712Z","url":"https://daily.dev/posts/B2K4Imgfa#c-457TskwiX","author":{"@type":"Person","name":"Matthew Spach","url":"https://daily.dev/mortybingo","image":"https://lh3.googleusercontent.com/a/AGNmyxabAIv37wPN55-WZxIVTcaqsGrmlYGhQtz0tXA51Lc=s96-c"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/here-is-how-to-write-better-frontend-code-b2k4imgfa#faq","mainEntity":[{"@type":"Question","name":"How do I clone a JavaScript object that contains a Date or a Map without losing type information?","acceptedAnswer":{"@type":"Answer","text":"Use structuredClone instead of JSON.parse(JSON.stringify(obj)). The JSON round-trip approach converts dates to strings and Maps to empty objects, silently corrupting data. structuredClone is a built-in browser deep-cloning algorithm, the same one browsers use to transfer data between tabs, and it preserves dates as dates and Maps as Maps. daily.dev surfaces practical browser API tips like this for developers cleaning up cloning logic."}},{"@type":"Question","name":"How can I lazy load images without a scroll event handler that calls getBoundingClientRect repeatedly?","acceptedAnswer":{"@type":"Answer","text":"Use the IntersectionObserver API instead of a scroll listener. The old approach recalculates each element's position with getBoundingClientRect on every scroll event, which can fire dozens of times per second, whereas IntersectionObserver registers each element once and lets the browser report visibility changes, cutting a two-second scroll from over 100 calls down to almost none. developers optimizing scroll-heavy pages often track browser API techniques like this on daily.dev."}},{"@type":"Question","name":"How do I style a card component differently based on the width of its container instead of the browser window?","acceptedAnswer":{"@type":"Answer","text":"Use CSS container queries by setting container-type: inline-size on the parent and applying styles with min-width conditions like min-width: 350px. Unlike media queries, which only respond to viewport width, container queries let the same reusable card component adapt its layout depending on whether it sits in a wide main column or a narrow sidebar. daily.dev helps frontend developers keep up with CSS techniques like container queries for reusable components."}}]}
```

