News for hackers. Stuff that matters.
Re:Don't Animate Height by deepsun on Tuesday July 22, 2025 @02:48PM (#44651415)
Just wrap it in a container with fixed height and "overflow: hidden".

Now the layout engine knows that it doesn't need to recalculate positions of elements outside that wrapper, and it's much faster.

By the way, the same trick was speeding up large <table> rendering back in the day. As long as you know the size of your rows or columns ahead of time, which kinda defeats the purpose of <table>.

Re:Don't Animate Height by codeptualize on Tuesday July 22, 2025 @04:05PM (#44652364)
This is the answer. I thought this was fairly common knowledge, height is animated quite often (think dropdowns), no need for the over complications.
Re:Don't Animate Height by liuliu on Tuesday July 22, 2025 @03:38PM (#44652040)
Thank you! This is what I am looking for in HN comments. Layout engine cannot be that stupid per the article. I guess it is not once you give it enough hint.
Re:Don't Animate Height by alexchantavy on Tuesday July 22, 2025 @05:02PM (#44652962)
Is there a good way to learn techniques like this from first principles other than trying to become an employed frontend dev?
Re:Don't Animate Height by RumourRider on Tuesday July 22, 2025 @07:28PM (#44654180)
You need to think about what the browser is actually doing when it displays a web page. You need to think about what happens when you type the URL in and what the browser is doing conceptually.

If you have any experience programming 2D graphics with SDL this will help you to understand what is happening as well.

There is an old school talk by Nikolas Zakas on JavaScript Performance from 2009 which while some of it is out of date now due to improvements in Browser engines, the fundamental ideas are still the same.

https://www.youtube.com/watch?v=mHtdZgou0qU

If you skip to the 35 minute mark he speaks about reflow specifically. Generally I still use many of these techniques when writing vanilla JS (which is unfortunately rare these days). There are other talks where he talks about many other performance specific topics.

Obviously none of this is as good as real world experience of dealing with a thorny issue.

Re:Don't Animate Height by troupo on Wednesday July 23, 2025 @02:33AM (#44656283)
> If you have any experience programming 2D graphics with SDL this will help you to understand what is happening as well.

No. No it will not. Nothing in 2D graphics will help you understand things like "simple animation required full layout re-calculation and re-painting of the entire page"

Re:Don't Animate Height by RumourRider on Wednesday July 23, 2025 @03:11AM (#44656514)
Yes it will. Most of the CSS styling is literally wrappers around draw calls.
Re:Don't Animate Height by troupo on Wednesday July 23, 2025 @08:31AM (#44658482)
Which draw calls result in "changing border width will cause a full page re-flow in some browsers, and bot others"?

Which draw calls are "literally any animation will cause the full re-flow of the document unless you make sure an element is not a part of the document, but then you'll have issues with positioning, weird gaps etc."

Which draw calls teach you that "well, this primitive animation consumes 15% of CPU, but will only consume 6% if you go out of your way to make it 'performant'"?

Re:Don't Animate Height by RumourRider on Wednesday July 23, 2025 @09:14AM (#44658871)
You are being disingenuous. I can always use some hyper specific example to disprove any statement about what is generally true. That doesn't mean that understanding what is typically happening is useless. Which is essentially what your claim is.
Re:Don't Animate Height by troupo on Wednesday July 23, 2025 @09:40AM (#44659143)
> You are being disingenuous. I can always use some hyper specific example

These are not hyperspecific examples. We're literally discussing under an article which has a primitive animation which was, quote, "using 60% CPU and 25% GPU on [a] M2 MacBook"

How is 2D graphics programming helping there?

The article goes on to describe, correctly, layout properties ("The W3C spec is full of these!"), paint properties ("this tiny "bikeshed" SVG which you can find on lots of W3C spec pages. It costs ~30% CPU!") and composite properties.

These are not specific examples. These are literally footguns upon footguns that literally have no corresponding counterpart anywhere.

> That doesn't mean that understanding what is typically happening is useless. Which is essentially what your claim is.

Yes, knowing 2D graphics programming is 100% useless to understand what's happening with CSS and DOM.

Re:Don't Animate Height by RumourRider on Wednesday July 23, 2025 @10:06AM (#44659383)
> These are not hyperspecific examples. We're literally discussing under an article which has a primitive animation which was, quote, "using 60% CPU and 25% GPU on [a] M2 MacBook".

No we are not. Someone asked about how one learns some techniques that the OP mentioned (that were simpler than the ones given in the article). I gave some general advice on how one should think about what is happening and some general advice about how to think about performance.

Then you are pretending that none of this is relevant because you are concentrating on the hyper specific stuff in the article (which we are no longer talking about).

I also disagree that this cannot be reasoned about by learning a bit about how graphics works, Cartesian coordinate system and literally turning on the paint flashing tool that is in dev tools. It is literally obvious what is happening when you see it.

> Yes, knowing 2D graphics programming is 100% useless to understand what's happening with CSS and DOM.

In fact it was very helpful to help me to understand what was happening. I literally said to myself "wait a minute, that is kinda like this thing I did in SDL 1.2 in university". You are literally telling me, that something I know to be useful isn't, because you say so. That is unreasonable.

Re:Don't Animate Height by troupo on Wednesday July 23, 2025 @12:20PM (#44660935)
> No we are not.

Yes. Yes we are literally discussing this under an article whose introduction literally says what I quoted.

> Someone asked about how one learns some techniques that the OP mentioned

Yes, and the techniques OP mentioned literally cannot be learned from 2D graphics because nowhere in 2D graphics are you going "ah yes, to fix 60% CPU utilization for a primitive animation, you need to apply this awkward workaround that will tell the browser not to reflow the page".

Edit: and this particular technique is only applicable to this specific property for reasons that do not exist anywhere else, except in the browsers.

> wait a minute, that is kinda like this thing I did in SDL 1.2 in university

Well, it's not. Just because it kinda somewhat looks like something different doesn't make it so. Hence my question in this comment: https://news.ycombinator.com/item?id=44658482

Because those are not hyper specific, no matter how much you pretend they are

Re:Don't Animate Height by brokencode on Tuesday July 22, 2025 @05:47PM (#44653374)
Usually you try to build something, realize it’s slow, then do a combination of searching for possible solutions, trying them, and profiling again and again to gain this knowledge.

Experience is the best teacher.

Re:Don't Animate Height by notatoad on Tuesday July 22, 2025 @07:02PM (#44654012)
not really. the web is 30+ years of fixes, shortcuts, compatibility hacks, tech industry politics, and other assorted mistakes.

the "first principles" to understand it all is the complete set of mailing list archives of the WhatWG, plus the archives of the bug trackers of all the major browsers.

Re:Don't Animate Height by socalgal2 on Tuesday July 22, 2025 @09:33PM (#44654905)
I don't think it would be different for any other system. Trying to make a piece of computer software that is completely responsive to big displays, small displays, touch screens, mice, etc is just a ton of work and a ton of small details you can't easily learn until you need to learn them.
Re:Don't Animate Height by notatoad on Wednesday July 23, 2025 @02:38PM (#44662507)
yes. i don't mean to unfairly criticize the web as a platform, i just know it's warts because it's the platform i work on.

my criticism is reserved for the concept of "first principles", as if there should be a cohesive and simple set of rules that you can build up all the knowledge you need from. that's just not really how any complicated system works: building a comprehensive understanding of a complicated system takes a lot of work and a lot of time.

Re:Don't Animate Height by troupo on Wednesday July 23, 2025 @02:57PM (#44662709)
It's not "completely responsive". And literally every other GUI is just as responsive when coded for those screens.

Have you never resized a window on a desktop OS?

Re:Don't Animate Height by eviks on Tuesday July 22, 2025 @11:17PM (#44655409)
Not sure, but if CSS first principle is parent sets width going down to children, but sets height going up from children, then you could see how children changing its height leads to layout recalc all the way up.

So the you need to fix that height /remove resize, then google on which technique to use?

Re:Don't Animate Height by dsego on Wednesday July 23, 2025 @03:14AM (#44656534)
Understanding the document's flow layout, block and inline elements and build on top of that.
Re:Don't Animate Height by legitster on Tuesday July 22, 2025 @05:26PM (#44653176)
As an old-school webguy, fixes like this article make me sad.

The average front-end person these days has so little respect for the DOM and then gets mystified about why the McMaster-Carr website is so good despite being build with ancient practices.

Re:Don't Animate Height by sema4hacker on Tuesday July 22, 2025 @08:44PM (#44654589)
I just went to the McMaster-Carr home page and right-clicked for "View Page Source". There's a LOT of code there.
Re:Don't Animate Height by adastra22 on Wednesday July 23, 2025 @05:31AM (#44657279)
All modular, I hope.
Re:Don't Animate Height by nonethewiser on Wednesday July 23, 2025 @10:08AM (#44659414)
Looks like its using YUI (a yahoo UI library) that was deprecated in 2011 in favor of YUI 3 (which is now deprecated as well). https://yui.github.io/yui2/docs/yui_2.5.0/docs/index.html

Im seeing a lighthouse mobile performance of 64 with Javascript execution time, unused js, legacy js on modern browsers, etc. as some of the top reasons.

Re:Don't Animate Height by dotancohen on Tuesday July 22, 2025 @11:29PM (#44655475)
A few years ago a client that I hadn't spoken to in half a decade called me to thank me. He wanted to update his website to work with tablets, checked before calling me, and realized that his website just simply worked. Being the caveman that I am, I wrote the damn thing using the simplest CSS possible. Simple equals compatibility in this field.
Re:Don't Animate Height by bartread on Tuesday July 22, 2025 @11:42PM (#44655539)
Well, and especially because the “fixed” version is still - if I’ve read correctly - consuming 12% CPU before any user interaction on a page that’s otherwise substantially a plain or possibly RTFish text editor.

How is that considered acceptable? It’s still going to drain the battery faster than is justified, even if at a somewhat reduced rate from the pre-fixed case.

The acceptable range would be something like 0 - 1% when no interactions are taking place.

I’d still deploy the fix in the article as an interim solution - better is after all better - but I wouldn’t stop there.

Front-end web devs need to have more respect for power consumption and battery life (proxies by CPU and GPU usage).

Re:Don't Animate Height by jaapz on Wednesday July 23, 2025 @06:12AM (#44657503)
Couldn't they just have used an animated gif or png, the amount of CPU used probably wouldn't even be measurable
Re:Don't Animate Height by cpmsmith on Wednesday July 23, 2025 @10:32AM (#44659660)
You might be surprised at the CPU usage of rendering a GIF. I'm not sure why, but I've previously noticed Slack takes noticeably more CPU when there's an animated emoji on screen.
Re:Don't Animate Height by naikrovek on Wednesday July 23, 2025 @08:48AM (#44658627)
> Front-end web devs need to have more respect for power consumption and battery life

they need to understand and believe that their wishes are not everyone's wishes, and that they alone are the cause of their own problems. Everyone else works hard to give them the creative freedom they demand, but for multiple decades now they have demonstrated over and over how they love shooting themselves in the foot at every opportunity.

> consuming 12% CPU before any user interaction on a page that’s otherwise substantially a plain or possibly RTFish text editor. How is that considered acceptable?

It's not acceptable, unless you are a front-end web developer. If you are a front-end web developer, you don't consider this a problem in any way because the page looks as it is intended to look.

Re:Don't Animate Height by atiedebee on Thursday July 24, 2025 @07:08AM (#44669313)
huh, that was probably the least infuriating filter I have encountered in a while.

It's frustrating how just changing a filtering option will reload the entire web page on most websites.

Re:Don't Animate Height by bfgeek on Tuesday July 22, 2025 @05:42PM (#44653329)
This likely more effective quite a few years ago, but not particularly important today.

Changing height typically only shifts elements, and browser engines typically wont relayout them due to position changes.

"overflow: clip" is also much more lightweight than "overflow: hidden"

Re:Don't Animate Height by spankalee on Tuesday July 22, 2025 @09:17PM (#44654813)
`contain: strict` is much better than `overflow: hidden` for this case, and implies `overflow: hidden`.
Re:Don't Animate Height by bobbylarrybobby on Tuesday July 22, 2025 @11:31PM (#44655481)
https://developer.mozilla.org/en-US/docs/Web/CSS/contain

Yes, looks like this property isn't just a hack, but was designed with exactly the performance considerations laid out in the article in mind. Animating height is only expensive when the element’s height affects layout, which it doesn't have to!

Re:Don't Animate Height by dotancohen on Tuesday July 22, 2025 @11:32PM (#44655491)

  > Just wrap it in a container with fixed height and "overflow: hidden".
That's a fix for a webdev who cares, not a fix for a browser vendor as GP was alluding to.
Re:Don't Animate Height by fiddlerwoaroof on Tuesday July 22, 2025 @11:52PM (#44655584)
I don't understand how you'd fix this, in general, for the browser: the browser can't just assume certain animations will never affect layout.
Re:Don't Animate Height by kadoban on Wednesday July 23, 2025 @04:23AM (#44656909)
It could _probably_ figure out a lot of cases via some kind of static or runtime analysis.

Like the example from the linked article, there's one animation involved essentially and if the browser does the hard work for the entire length of one animation cycle and no layout ever changes, just assume it never will? It would have to have a bunch of edge cases to handle correctly I'm sure though.

Re:Don't Animate Height by deepsun on Wednesday July 23, 2025 @03:56PM (#44663304)
Browsers layout engines are pretty smart, and also optimized for speed at the same time, which makes them very complex. I can do one or the other in my code, but both is too hard. Now add there security.

But Google having more people working on Chrome than on Android I would say all the low-hanging optimizations have already been optimized, and all that's left is pretty complex even for teams of thousands.

Re:Don't Animate Height by troupo on Wednesday July 23, 2025 @02:31AM (#44656271)
Translation: just make a completely different functionality and effect than what was originally intended because the DOM can only handle static content, barely.
Re:Don't Animate Height by kadoban on Wednesday July 23, 2025 @04:26AM (#44656921)
The suggested change wouldn't affect functionality or the effect. It just tells the browser that there's a thing around the changing part with a fixed size that can't possibly change layout of anything around it, to the user it's invisible.
Re:Don't Animate Height by troupo on Wednesday July 23, 2025 @08:28AM (#44658449)
It will literally change the intended functionality by, placing it in a container of fixed height and overflow hidden