7 ms·
Everything You Need to Know About the CSS will-change Property
- untog 12y agoI feel like this is a great example of the... continuing OK-ness of the web as a platform. It feels hacky, but it's considerably less hacky than using unrelated CSS properties, so it's progress and I'm happy with that.
- eli 12y agoWhat would be an example of a non hacky way of hinting to the browser which properties are likely to change?
- untog 12y agoIn my dream scenario there's no need to do hinting at all. Most other platforms don't require it. But I am living and working in the real world, which has a significant legacy of web technology.
- eli 12y agoThe web doesn't require it either--we've made it this far, after all. I bet every platform has developers who wish they had simple tools to boost performance.
- resu_nimda 12y agoThe web doesn't require it either--we've made it this far, after all. That is a perfect encapsulation of the "OK-ness" he's talking about. Yeah, it works just fine. Yeah, the web doesn't "require" a smart graphics processing engine, it probably didn't "require" videos or the canvas or CSS transitions at all. But "really good" modern platforms have these capabilities, and IMO there's nothing wrong with wanting or trying to push the web to be as good a platform as possible, and it is frustrating (but understandable) that there is so much legacy baggage "holding it back."
- TheLoneWolfling 12y agoAn important point, however. One should not assume that all platforms have these capabilities!
- eli 12y agoI get that sentiment in general, but what does it have to do with a new hinting feature? Would hinting look different without legacy baggage? Would it not be needed? I don't see how it's possible to create an ideal browser that is fast at everything and also memory efficient. And there are always legitimate reasons to want to juice the most performance out of your pages.
- claudius 12y agoI can’t think of another platform (in the sense of programming environment) which is being optimised to be read milliseconds before execution from a networking socket, with more code being loaded on-the-fly from remote servers. In C++, you have all your code at compile-time and even in Python/Ruby/Perl/Bash etc., it would be highly unusual for the shell to anticipate the equivalent of wget -O foo ${URL} && . foo. Given the constraints, having to add some hints doesn’t seem all that bad to me.
- pcwalton 12y agoiOS definitely has such a thing via Core Animation layers.
- nkozyra 12y agoSaddling every element in the DOM with a data-will-change="TRUE" or data-might-change="POSSIBLY" and utilizing a heavy jQuery plugin to inject browser-specific CSS rules.
- gibbitz 12y agoHow about "look ahead"? If there are transition or keyframe properties related to variations of the selector, why not do this automatically? I understand the overhead, but from the standpoint of hackiness this would be the technology working as expected.
- eli 12y agoHow could a browser "automatically" know what changes I'm going to make to the DOM in a dynamic web app? Having the browser try to guess what transitions will be needed in the near future based on "related" selectors sounds way more hacky than a straightforward property that declares it. As the article notes, for all but trivial examples, these are likely to be added via Javascript. The static CSS file with ":hover" and ":active" was jsut an example.
- wildpeaks 12y agoSmall gotcha for people who have been keeping an eye on that from afar for a while: make sure to notice the property is now called "will-change", not "will-animate" anymore.
- brianzelip 12y agoHere's the W3C draft doc on the `will-change` module: http://dev.w3.org/csswg/css-will-change/ http://dev.w3.org/csswg/css-will-change/
- mathias 12y agoThe post links to it as well.
- gren 12y agoI'm a bit skeptical about this new property. In this basic example, .element:hover { will-change: transform; } .element:active { transform: rotateY(180deg); } it seems very predictable. I don't get why the browser couldn't guess (with some heuristics) what to prepare. Also I feel we will misuse this feature and tend to make the performance worth than if the browser would detect what are the "will-change"s.
- untog 12y agoWhich works fine in a CSS-only environment. But a lot of the time these properties are being set in JS, which is going to be very difficult to predict.
- troels 12y agoI generally try to not manipulate properties directly through javascript, rather declaring a class for the purpose. Doesn't seem like an unreasonable requirement for proper performance? (I mean, compared to having stuff like `will-change`)
- untog 12y agoIt is an unreasonable requirement in places, though. Before now I've made things like swipable elements that track the user's touch - i.e., you touch down, pull your finger left and the box follows you. That's not possible to do in pure CSS.
- bryanlarsen 12y agoBut you still use Javascript to apply the class to the element, right? So how can the browser know that your Javascript might do that? It seems to me to know that a rule along the lines of "might-add-class: foo;" would get us back most of that separation of presentation and content.
- Steuard 12y agoIs there a way to make this a JS-only interface or property, then? Heck, the article made it sound like using it directly in stylesheets would often be a bad idea anyway.
- Steuard 12y agoI hadn't heard about this before; I'm clearly out of the loop these days. With buy-in from smart people at Webkit and Mozilla, I've got to assume that it really will be an improvement over the status quo. But, boy, it feels awkward at first sight. To my eye, it's weird to see what is essentially a low-level optimization instruction as part of CSS (which is usually all about presentation rather than implementation). Also, the article makes it clear that there are a lot of ways that people could mistakenly use this property in unhelpful ways. And I'm still not entirely clear on what they mean by "It’s not possible to remove will-change if it is declared in the style sheet": is this really a CSS property that can be turned on without scripting but never turned off without it? That makes me uncomfortable. So why isn't this a case where we just need browsers to get smarter? In the same sense that an optimizing compiler can in most cases produce better performance than a human writing machine code by hand realistically could, can't we expect browsers to gradually get better and better at spotting these rendering optimizations? (To take an example from the article, couldn't the browser recognize that there's a transform rule for :active and automatically enable something equivalent to will-change for :hover itself?)
- bgirard 12y agoI made the original proposal for will-change on Mozilla' behalf. I agree with your concerns, we've had them since 2010 and kept pushing back on proposing this. But with HiDPI mobile and 4k desktop it's really difficult without it. It is nearly impossible to predict changes that some page do in perfectly reasonable ways, especially for changes triggered from Javascript. It's true that in some cases a will-change can be inferred from the style sheet but there are still too many changes triggered from JS events. You wont lose any performance by not using will-change and we will continue improving how we infer them. Once an element is declared as will-change the browser will be able to weight the hint and the platform specific costs of various possible optimization like layerizing the element and ignore it if it isn't suitable for the device. If we can't predict the change then we're left trying to be ready to change a new element within one vsync interval to avoid any latency. This is fine if you're dealing with a small element but large elements like a fullscreen CSS page flip triggered from JS is going to be nearly impossible to perform without a frame or more of latency. In the case of HiDPI mobile you typically have 2 millions pixels (8 MB) to deal with on a 1GB/s memory bus with a frame time of 16ms. That means that if you touch 16 MB, or twice the size of the screen, then you have zero chance to perform that change without introducing delays. And this is without even considering the CPU/GPU cost require to perform the change. The situation may also get tough on 4k desktop without a discrete GPU if you have 31MBs per fullscreen surface on a 10GB/sec bus. These are all theoretical throughput maximums. In practice on Android and Firefox OS where a style change occurs on large and complex elements where rasterization is difficult you can't use SIMD to rasterize the page at memory bandwidth speeds, you instead are bottlenecked on rasterizing complicated content. It's typical to see these complex change cause 100ms latency. Once the rasterization is made then the animations can continue smoothly but you're still left with animations skipping at the beginning.
- jhherren 12y agoPutting performance optimization hints in my presentation code feels wrong. I think I'd use a separate css file for will-change.
- talmand 12y agoBy what I'm reading you shouldn't have the property in a style sheet at all. The author implies that every usage of the property requires resources that are never released as long as the property is still active. The property in a style sheet will mean that the resources are never released. Thus, the suggestion to use the property through javascript so that it can be applied and then removed. That just seems totally weird to me.
- dmmalam 12y agoShould'nt this be something that the browser should auto-optimise with a smart renderer? In the example given with the element:hover and element:active, it would trivial to parse all possible CSS transforms. Then live profiling of the page would give data on the likelihood of particular transforms. The browser could then optimise as necessary given hardware limitations and current resource constraints. Even transforms added via javascript or onhover would be caught via live profiling. The given use case of stopping flickering when a layer is created should be treated as a bug. This feels hacky, and likely to be used erroneously or abused - at which point browser will be forced to smartly ignore leaving us with yet more cruft. Performance tuning is a fine art, and browser is in a better position to do it vs your average web developer.
- Pxtl 12y agoI would hope that the browser would parse all CSS transforms and then use this particular attribute just for Javascript-based transforms.
- IgorPartola 12y agoI don't see it. If I create something like this: .wide { transition: width 2s; width: 300px; } but don't apply it to any element, then load a remote JavaScript file that adds that class dynamically, no rendering engine will be able to apply this optimization.
- Pxtl 12y agoI was thinking of animations that are applied declaratively in the HTML/CSS itself, not javascript - things like hover.
- IgorPartola 12y agoThe animations that are applied dynamically are the interesting thing. They are the ones more likely to end up being used in performance-critical applications.
- IgorPartola 12y agoThe real question is when will mobile Safari, mobile Firefox, and mobile Chrome support this?
- frik 12y agoonly WebKit and Firefox Nightly builds have implemented [it] [...] There is also an intent to ship in Blink too. (Opera is based on Chromium that uses the WebKit fork "Blink") Offtopic: first time in years, I see the opera.com website. There was a lot of useful information in their blog posts, when they still worked on their own browser engine.
- danatkinson 12y agoIs there a test suite/page for the will-change property in order to gauge vendor compliance?
- general_failure 12y agoWhile I understand the need for the property, it is absurd to put this in a style sheet document :( oh well I have mostly given up on this doc/style/code separation anyway
- timothya 12y agoA lot of people are complaining about putting this into CSS, but I think that people are forgetting that using null transform hacks[0] is already the status quo for making animated elements more performant. What this property does is lets us continue doing the same thing, but in a way that the intent is clear ("I'm using this property to make the animation on this element more performant" vs. "I want to add a transform that does nothing"). Furthermore, it lets the browser decide if it wants to add any optimization, and it can decide which sort of optimization is best. While it would be nice if we didn't have to do this hinting at all, the fact of the matter is that it's hard for the browser to predict what will happen on the page in the future, and we're already having to do this (just in the cryptic null transform way), so it's nice that the web platform is letting us do it in a standardized way that communicates our intent clearly. [0]: http://aerotwist.com/blog/on-translate3d-and-layer-creation-hacks/ http://aerotwist.com/blog/on-translate3d-and-layer-creation-...
- mattewilliams 12y agoTLDR; Everything I "need" to know? Doubt that
- pothibo 12y agoI don't know why, but to me it feels like that will-change property should have been associated to the HTML, not CSS. <section class="animatable" will-change='transition'></section> section.animatable:active { // someanimation } The above makes more sense to me than the following: section.animatable { will-change: 'transition'; &.animating { // Some animation } }
- danabramov 12y agoI don't understand why people complain about this. It makes perfect sense: when you change your CSS (maybe adding `opacity` to `transform: translate`), you will notice this property and remember to update it. If it stays in your HTML or JS, you (or whoever changes CSS later) won't keep it up-to-date and the optimization hint will be wrong (which can hurt performance).
- KyleLH 12y agoI haven't done much with CSS transformations, but I really like this change and think it's a step in the right direction. So much of the web development I've done has involved doing weird hacks to achieve the desired result, and I think that making will-change a valid attribute will help make things more clear. Hopefully this trent will continue.
- ultimatedelman 12y agoWhile it may be functionally great, it just smells wrong to have a CSS property that you're pretty much only supposed to set via JS that you're supposed to remove after you've set it.
- EGreg 12y agoIs it a commentary on the state of affairs that I was more impressed to see this written by a Female Lebanese web developer than I was at the content?
- lodewijkadlp 12y agoThis is a hack to patch a bug produced by optimization on the browser side. I get that it's a neat hack. I really do. But this should never become standard. It just shouldn't. I guess it beats having a videocard optimized for rendering webpages, which is probably the alternative. Can we just clean slate reboot HTML/CSS/JavaScript to something that's quite like it but has reinvented seperation of concerns? Symantics/Syntax/Dynamics was kinda neat. Right now it's "State/Looks/Processing", and it's backwards compatible in a messy way. I seriously think that HTML5 is just too cluttered, with no cleaning up going on. Compiling towards HTML5 is not improving the problem, and if we go down that road I suggest making languages better for compiling towards web-compatible(Hey, c, how are you? What's that? You're in browsers now?).
- exo762 12y agoMostly technical article about CSS opening with explanation of what CPU and GPU are and where they are located. I'm very surprised that those two terms even need explanation.
- gpvos 12y agoAcceleration using the GPU is what will-change is all about. Also, especially among web developers there are many people who barely know what they are doing, so it's a good idea to comprehensively cover the context (I know it helped me, since I'm mostly a back-end developer who does some front-end on the side).
- tempodox 12y agoI love this article. It is well written and it even has a date. Thanks :)