javascript: developing for performance
By Anatoly Mironov
Many words have been said about the importance of performance when working with javascript files. Here I want to summarize what developers can do to increase the performance related to javascript. I found many tips on blogs. Here comes my aggregated list of actions one can do to speed up sharepoint (and not only) sites with focus on javascript:
1. Load only sharepoint stuff you need
Use prefetch option to only load sharepoint javascript files that needed. To see the difference, just add ?prefetch=0
to your sharepoint url in the browser. The downside of this lazy loading is that you wrap all sharepoint related javascript into ExecuteOrDelayUntilScriptLoaded otherwise you maybe invoke some of javascript objects and functions that are not loaded. So the prize of this huge performance improvement is a high awareness by a developer when and which scripts are loaded and run.
2. Load only custom stuff you need
The same lazy loading principle should be used for your scripts as well, unless your javascript code is little. If your projects have much of functionality on client side, than you can increase loading performance enormously. This post is not about details, but when you want to know more about “On Demand javascript”, take a look about SP.SOD, head.js, require.js.
3. Minify all your scripts
Minify or crunch your javascript files. There are many tools available for doing that: google closure compiler, jscrunch… But the best javascript minifying tool for SharePoint development I found is Mavention SharePoint Assets Minifier which is developed by Waldek Mastykarz and highly integrated with Visual Studio and SharePoint debug/release modes.
4. Minimize your scripts
Not only how much javascript is loading matter, but how many files as well. Like sprites in CSS, you can combine your scripts into one that you load. Just organize your scripts as they were classes (one class - one file), then use T4 to generate a composite javascript file.
5. Check your javascript for memory leaks
Thinking about memory leaks is important, and not only in SharePoint Server Side. It is important to avoid memory leaks in javascript as well. Especially when your audience uses older browsers. See this amazing post about javascript memory leaks on Ying’s blog. This is all so far. Have I missed something, please comment.