Web performance (Desktop, Mobile or Tablet)
- Network (connectivity and bandwidth)
- CPU (Hz) — Metric
- Time to Glass (Amount of time a user takes the action and site is loaded on the glass)
- Elapsed Page load (When user clicks on link and page loads and CPU is calm/acquiesces)
- CPU time (How much time used)
- Idle CPU time (CPU has been wasted)
- GPU
Introduction
- Myth — The page load (fast) is not proportional javascript lines of code.
The number of elements and images are also not proportional to speed either.
Instead the speed is a measure of how fast the DOM is ready with initial setup and users see something they can make sense of on the screen.
2. Decrease the CPU time and increase parallelism within the subsystems in the sequence.
3. Myth — Faster the javascript engine, the faster the AJAX application will be. The pipeline from step 6–10 are important, javascript has a small role.
Sequence of events (on browser) –
- Networking (Bring resources, cache, and backup)
- HTML (Parse, represent internal data structure as DOM)
- CSS (Parse and format data)
- Collections (Metadata – internal use during runtime)
- Javascript (Parse, generate byte code or IL)
- Marshalling (Boundary between script engine and browser) – Time is spent in communication/interaction.
- DOM (Manipulate nodes, look-up elements)
- Formatting (Take markup and CSS and overlay each on top of each other)
- Block Building (CSS is inherently is a block based layout sub system)
- Layout (Take the blocks rectangles and put it in place on the screen)
- Rendering
Six Principles
1. Quickly respond to network requests
- Avoid 3xx Redirections. This can cause up to 200ms delay (10% of delay).
- Avoid meta refreshes. <meta http-equiv=“refresh” content=“url=http://news.com/”/>
- Server response time should be measured.
- Use Content Distribution Networks (CDN’s). Metric – Atlanta to Redmond – assuming hops (250ms)
- Browser can make 6 on an average per domain. So if images are on in different domains – you can make 24 resources in parallel.
- Reuse Connections (In the HTTP header please don’t send Connection-Close) – some servers do.
- Know your servers (minimize the impact of response)
- Understand your network timing.
Unload-Redirect-AppCache-DNS-TCP-Request-Response-Processing-onLoad
2. Minimize bytes downloaded
- Average websites downloads 777k. Images, Scripts, HTML, Style Sheets – in that order
- Gzip your content – make sure from the HTTP header that the server is doing that for you.
- Persist App Resources Locally in Package. (This is Windows App Store resources)
- Cache Dynamic Resources in App Cache (Use HTML5 app cache) – version v1, v2.
- Provide Cacheable Content. Put the Expires in the HTTP header for the resources.
- Send conditional requests (Mark content as cacheable and set expiration, use Last-Modified in HTTP header)
- Cache Data Requests (jQuery.ajax({ has a property cache:true)
- Standard File Capitalization Convention – Lowercase, uppercase, carelesscase. (Three different network resources for each)
- Use HTTP header and page meta=tag if an old website needs to run in the IE7 mode.
- Always link style sheets at the top of the page, ideally in <head> also synchronously BLOCK painting the page.
- Avoid using @import for hierarchical styles. Synchronously blocks the painting to the screen.
- Avoid embedded and inline styles. Browser has to context switch from the CSS parser to HTML parser. Even fixing the small bug in there – NOT OK!!!! (save yourself 15–20%)
- Only Include Necessary Styles. Same style sheets for the same page – NOT RECOMMENDED. The browser has to download it and create all the data structure that will never be used.
- Always Link JavaScript at the End of File. Javascript engine to kick off later.
- Avoid Linking JavaScript in head. (Synchronously block on javascript)
- Avoid Inline JavaScript (same context switching – much larger degree – jitting runtime issues)
- Use the Defer Tag if it is necessary to put the javascript in the HEAD tag.
3. Efficiently Structure Markup
<script src=“myscript.js” defer=“defer”></script>
- Asynchronously Download Script. Use ASYNC keyword in the script tag.)
<script async src=“myscript.js”></script>
- Reuse the javascript code.
- Standardize on a Single Framework (lot of framework does the same thing)
- Don’t include script to be cool.
4. Optimize Media Usage
- Minimize number of images. (Average website downloads 58 images)
- Avoid Death by 1000 Images – especially from so many different domains – DNS delay.
- Use Image Sprites. (See metric in slide) – one image sprite for icon. More images – more connections.
- Create Your Sprites by Hand – most efficient.
- Image Formats: PNG, JPEG, JPEG-XR – in that order.
- Use Native Image Resolutions – do not scale it down on client.
- Replace Images with CSS3 gradients – old way of doing it is having an image instead.
- Replace Images with CSS3 Border Radius.
- Leverage CSS Transforms – instead of different images.
- Use DataURI’s for small single view Images.
- Avoid Complex SVG Paths
- Video: User Preview Images – only downloads the image not the entire video.
- Minimize media plugin usage. (more runtimes, more slow) – HTML5 is the best.
- Proactively Download Future Media – asynchronously.
5. Write Fast JavaScript
- Stick to Integer Math. Floating point is slow on javascript – convert floating numbers to integers Math.floor.
- Minify your JavaScript. Smaller variables and minimized code.
- Initialize JavaScript on Demand. (see example)
- Minimize DOM interactions.
- Built-in DOM Methods Always More Efficient (even compared to third-party library – firstchild, nextsibling)
- Use Selector API for CollectionsAccess — document.querySelectorAll(“.required”); FASTER
- Use .innerHTML to Construct Your Page (10 to 15 times faster)
- Batch Markup Changes (don’t change the first name and them last name, etc)
- Maintain a small and Healthy DOM (recommended is 1000 elements)
- JSON is always faster than XML
- Use Native browser JSON methods
- Use Regular Expressions Sparingly.
6. Know what your application is doing
- Understand JavaScript Timers
- Combine Application Timers
- Align Timers to the Display Frame
- Use requestAnimationFrame for Animations
- Know when your app is visible. (Page Visibility API) – HTML5
Reference
I loved the BUILD 2010 video - 50 performance tricks to make your HTML5 apps and sites faster
You can find the same in here -
http://channel9.msdn.com/Events/Build/2012/3–132