GOAL : 50 /60 frames per second
NOTE: 16-20 milliseconds to compute and draw each frame
Optimisation Tools
- “display:none” to objects that are not visible
- Minimize nesting in the DOM
- Use a 2D transform (“transform:translate(x,y)”) rather than “top:y” and “left:x”
- Batch geometry reads from the DOM and modification of the DOM and avoid interleaving reads and writes
- Constrain all animations within a render layer
- Avoid overlapping render layers
- Add elements to the DOM using DOM APIs => avoids reparsing the existing HTML
popupDiv.appendChild( document.createTextNode(”Much better”) );
document.body.appendChild(popupDiv);
- Create a DOM fragment and then add the fragment to the DOM
LAYOUT CALCULATION
.offscreen > * {
display:none;
}
NOTE:
Changing one of these styles trigger layout
Width Height min-height
Padding Margin Bottom
Display border-width Clear
Border Top white-space
Position font-size vertical-align
Float text-align Right
overflow-y font-weight line-height
Overflow Left font-family
Note: Use transform instead of top:x or top:y
Use a 2D transform (“transform:translate(x,y)”) rather than “top:y” and “left:x”
Another source of layout that is often overlooked is requesting the geometry of an object from JavaScript. Requesting the size or position of a DOM element forces the browser to perform layout before it can provide the answer to JavaScript.
// Read (might require layout)
var h1 = element1.clientHeight; // Write (invalidates layout)
element1.style.height = (h1 * 2) + 'px'; // Read (triggers layout)
var h2 = element2.clientHeight; // Write (invalidates layout)
element2.style.height = (h2 * 2) + 'px'; // Read (triggers layout)
var h3 = element3.clientHeight;
// Write (invalidates layout)
element3.style.height = (h3 * 2) + 'px';
- Batch geometry reads from the DOM and modification of the DOM and avoid interleaving reads and writes. However, a better solution is to use CSS3 transition and animation to animate elements of the user interface.
- Using CSS3 transition and animation gives the browser information on how the element is being animated. This allows it to use hardware resources when available to maximise performance.
Note: Animating using JavaScript uses the main thread, using CSS allows the work to be off- loaded to the compositor thread
- In situations where the only option is to drive the animation directly from JavaScript, batch all modifications to the DOM within one callback from requestAnimationFrame.
The requestAnimationFrame function allows a function to be called at the next vertical sync. This allows changes to the DOM to be handled in one place, once per frame.
window.requestAnimationFrame = window.requestAnimationFrame ||
function(fn){ window.setTimeout(fn, 20); }
FONT RENDERING
-
“display:none” is set on all text elements that are no visible on screen.
Becareful about text reflowing
The browser will divide the page in to render layers and produces paint buffers for each layer. On platforms with a GPU, there will be one or more composition layers that are linked to graphics textures in the GPU.
- A common technique to cause a composition layer to be created is to add a zero 3D transform to a div.
-webkit-transform: translate3d(0,0,0)
At the time of writing, the follow rules apply to WebKit’s decision logic for choosing render layers:
-
The element is an SVG root (<svg>).
-
The element is a text or search <input> field.
or:
-
The element does not have a percentage height value.
-
The element does not have an implicit or auto height value.
-
The element does not have an implicit or auto width value.
-
The element has an explicit overflow value (scroll, auto or hidden).
-
The element is not a descendant of a <table> element.
-
The element is not set to display inline or inline-block
- If you do transition fade out... then when zero set display to none