One of the possible ways to reduce the load on CPU, is to use a so called null transform hack
, which is often hailed as something of a silver bullet. In many cases it will drastically improve rendering performance in WebKit and Blink browsers like Chrome, Opera and Safari.
Usage of the "Null transform hack" (a hardware compositing mode)
The null transform hack basically does two things:
- It switches on the hardware compositing mode (assuming it's supported for the platform)
- It creates a new layer with its own backing surface
To "force" a browser, simply add one of these CSS properties to the element:
transform: translateZ(0);
/* or its friend: */
transform: translate3d(0, 0, 0);
When working with 3D transforms, it's good to have these properties as well to improve the performance:
backface-visibility: hidden;
perspective: 1000;
Caveats of the "null transform hack"
Enabling a hardware acceleration in CSS3 for a lot of objects may slow down performance!
Apparently, each null 3D transform creates a new layer. However, force-hacking layer creation may not always be the solution to certain performance bottlenecks on a page. Layer creation techniques can boost page speed, but they come with a cost: they take up memory in system RAM and on the GPU. So even if the GPU does a good job, the transfer of many objects might be a problem so that using GPU acceleration might not be worth it. The cite from W3C:
However, setting up the element in a fresh layer is a relatively expensive operation, which can delay the start of a transform animation by a noticeable fraction of a second.
Moving a few big objects has a higher performance, than moving lots of small items when using 3D-acceleration. So they must be used wisely and you need to make sure that hardware-accelerating your operation will really help the performance of your page, and that a performance bottleneck is not being caused by another operation on your page.
Moreover, a GPU is designed specifically for performing the complex mathematical/geometric calculations, and offloading operations onto the GPU can yield massive power consumption. Obviously, when hardware kicks in, so does the battery of the target device.
The modern way: the will-change
property
The progress is not standing on the one place... W3C introduced the will-change
CSS property. To cut the long story short, the will-change
property allows you to inform the browser ahead of time of what kinds of changes you are likely to make to an element, so that it can set up the appropriate optimizations before they're needed.
Here's what they say in the the draft:
The will-change
property defined in this specification allows an author to declare ahead-of-time what properties are likely to change in the future, so the UA can set up the appropriate optimizations some time before they’re needed. This way, when the actual change happens, the page updates in a snappy manner.
Using will-change
, hinting to the browser about an upcoming transformation can be as simple as adding this rule to the element that you’re expecting to be transformed:
will-change: transform;
When developing for mobile, developers are forced to take the wide array of device constraints into consideration while writing mobile web apps. Browsers are becoming smarter, and sometimes, it's better to leave the decision to the platform itself, instead of overlapping acceleration and forcing the behavior in a hacky-way.