Today I looked into the performance of different layout modes in CSS. I was curious if it takes the browser longer to lay out elements in a grid vs. Flexbox vs. flow layout.

TL:DR; they're all plenty fast ⚡️ but there are small differences.

Info in thread 👇🏻
For the benchmark, I created an app that renders 1000 random items, with a couple HTML tags in each one. I trigger a re-render, and measure how long it takes to recalculate layout.

Specifically looking for the time spent in this step: Close-up screenshot of the ...
1. Regular flow layout. No CSS on the container.

Average layout recalc time: 22.3ms

I imagine this is the lowest it could be, since each element fills the available width. No "interplay" between children.

This will be our baseline! A list of names stacked ver...
2. A flex column.

Very similar layout to the last element, except that margins don't collapse anymore.

Average layout recalc: 22.8ms (+2% slower, but really it's a wash).

Makes sense, browser isn't doing much different! A list of names with slight...
3. A flex row, with `flex-wrap: wrap`, and min/max sizes on children

This one requires a bit more computation, since the browser has to decide where the lines wrap.

Avg: 26.5ms (+19% slower) A list of names, except the...
4. A 5-column grid.

CSS:
display: grid;
grid-template-columns: repeat(5, 1fr);

Avg: 28.9ms (+30% slower)

I remember reading that CSS Grid could be slower in some circumstances, but it doesn't make a HUGE difference. A list of names split into ...
5. A CSS grid using the auto-fit trick.

CSS:
grid-template-columns: repeat(
auto-fit,
minmax(8em, 1fr)
);

Avg: 25.8ms (+16%)

I'm surprised that this one is slower than the 5-col one! It seems to be much more dynamic.

I repeated the experiment, same result. Image
These numbers are all recorded on a powerful iMac Pro, but I repeated the tests* on my budget Xiaomi Redmi 7A phone, a popular low-end smartphone in India.

Quickest (Flow layout): 149ms
Slowest (5-col Grid layout): 174ms (17% slower)
* I couldn't be fussed to figure out how to get Chrome devtools running on Android, so I instead measured the entire React re-render time. On PC, the layout recalc is about 20% of the total time, so I took 20% of the mobile re-render times. Not perfect, but probably close enough.
My takeaway: there is a cost when it comes to using dynamic layouts, but that cost is quite small in absolute terms.

Even on a low-end device, using Grid layout probably won't increase render times by a noticeable amount 😄
There is a caveat, which is that synthetic benchmarks are synthetic. Your app might have a very different tree shape, which might brush against edge-cases not present here.

Every situation is unique, which is why it's important to profile!
I've seen a couple "CSS Grid is slow, use Flexbox instead" takes, and I feel much more comfortable now saying that you should ignore them 😅 the folks working on browsers are super smart and they wouldn't ship a layout method with huge performance problems.

Grid it up!
Check out the benchmarks and run your own tests here: codesandbox.io/s/grid-perf-be…

Also, if you're interested in CSS performance, sign up for my newsletter! I'll be publishing more on this subject on my blog 😄

joshwcomeau.com/subscribe/
UPDATE: @brian_lovin reminded me that he experienced a very real perf issue with CSS Grid, so I did a bit more digging.

With nested grids, there IS a not-insignificant perf hit.

New benchmark: oczpm.csb.app

Grid version: 127ms
Flex version: 79ms A grid full of grids, with ...
This demo has the following structure:

Grid
- Grid (x1000)
- Grid
- Grid
- Grid

4001 total `display: grid` calls 😮

Given the sheer volume, I'm actually kinda impressed with how well it held up 😅
I think my takeaway is the same: don't be afraid of Grid, use it when it makes sense to use it. But do check to make sure it doesn't cause problems for you: there may be times where it's slower!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Josh W. Comeau

Josh W. Comeau Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @JoshWComeau

12 Jan
🌠 Keep navigation centered in a lopsided container (logo on one side, nothing on the other) with Flexbox!

“Asymmetrical centering” like this has befuddled me for a long time, but I've cracked the code: `flex-basis` is key 🔓🗝

Demo + Code: codepen.io/joshwcomeau/pe…
When `flex-basis` is set to `0`, it distributes ALL space, not just empty space.

So: we put columns on either side of the nav, make them greedy with `flex-grow`, and distribute the space with `flex-basis`:

.column {
flex-grow: 1;
flex-basis: 0;
}

(Image credit: w3 dot org) A diagram showing how flex-basis: 0 distributes all space re
(You can also use `flex: 1` instead, but that shorthand obfuscates what's really going on here!)

Another nifty trick: The nav items "fan out" as the window gets larger, thanks to viewport-based gap sizes:

nav {
display: flex;
gap: max(calc(10vw - 60px), 16px);
} Two side-by-side screenshots of a large window and a small o
Read 6 tweets
6 Jan
Woaaah — An AI that can generate delightful illustrations from text captions.

This will be GOLD for folks who want custom illustrations to visually explain a concept. The content creator in me is *pumped*.

openai.com/blog/dall-e
Heard from some folks that are concerned about the effect it'll have on artists. Wrestling with this a bit.

In my view, it's similar to the effect that Squarespace had on web developers. There's still plenty of demand for us, but it changed a bit.
Essentially things shift upwards a bit. It's less common to get paid $500 to build a website for your local bakery, but still plenty of opportunities building dynamic products for businesses with big budgets.
Read 10 tweets
9 Dec 20
✨ This year I decided to take blogging more seriously. In 2020, I published 30 articles on my blog, joshwcomeau.com.

We're nearing the end of the year, so I thought it'd be fun to do a "Top 10" countdown, sorted by # of views 😄 I'll share some context about each one!
#10 — Boop! joshwcomeau.com/react/boop/

This animation tutorial details how I created a wobbly temporary hover state using React Spring.

I was unhappy with the hover animations on my course platform, and decided to experiment. I was so happy with the results, it became a blog post!
#9 — Embracing Modern Image Formats. joshwcomeau.com/performance/em…

A look at how to use the modern “.webp” image format to shave some KBs off your page, in a backwards-compatible way 😄

Tools like `gatsby-image` and `next/image` are making it redundant, and I'm thrilled ✨
Read 13 tweets
23 Nov 20
The most infamous CSS rule is:

button:focus {
outline: none;
}

This style removes the "ugly" focus ring on inputs and links, but it means keyboard users can't navigate; it totally breaks the experience for them (and any non-mouse users) 😬

A thread with some solutions 🧵
First, some quick options:

• Leave them as-is! Outlines aren't so bad.
• Provide an alternative focus style, like changing the background color, or adding a border.

There's a nifty new pseudo-class you may not be aware of, though…
🌠 The `:focus-visible` pseudo-class is just like the `:focus` one, but it only matches when the browser determines that a visible focus state is necessary (eg. not a mouse user).

Keyboard users will see the focus rings, but mouse users won't.
Read 6 tweets
31 Oct 20
For the past week I’ve been using the Xiaomi Redmi 8 as my main phone. It’s the most common “budget” smartphone in India.

In some ways, this thing is shockingly good. In others, downright surprising. But there were some disappointments.

🧵 My experience in-thread: ImageImageImage
For context, I ordered this phone from AliExpress. Cost me $90 USD.

It costs ₹8,000, well below the average Indian smartphone price of ₹12,000 / $160.

Source: statista.com/statistics/809…
Quick caveat: I’m not pretending that my internet experience has been identical to that of an average Indian smartphone-user

The average mobile internet speed in India is 12Mbps, roughly 1/3rd global average. But unless you’re video-streaming, 12Mbps seems OK to me?
Read 16 tweets
6 Jul 20
At a couple jobs now, I've been tasked with reviewing incoming applications, so I've read _a lot_ of cover letters. IMO, a lot of them miss the mark 😬.

Today I thought I'd share some cover letters I've written which led to an offer, + some cover letter tips.

See thread 🧵 👇
All cover letters can be read in text here: docs.google.com/document/d/1a6…

Letter 1: Khan Academy.

This letter isn't my best work, but it tells a compelling story about why I'm passionate about the space + the organization. Could be improved by sharing more about my skills/background Screenshot of doc linked in tweet
Letter 2: Glitch

I'm proud of this letter. I cover why I care about their mission (online code education), their product (mentioning specific features), and why my skills/background are valuable to them in their mission / for their product. Screenshot of doc linked in earlier tweet
Read 12 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!