JavaScript Performance Tuning
Jeff D. Conrad
09/08/2011
Resource loading strategy
- Required load (style then javascript)
- Ready state
- Pre-load/lazy loading
- Action-dependent/info-dependent
<li id="prd_0" class="productView first">
<img class="thumb"
height="147"
width="187"
src="images/clear.gif"
data-lazyimages-src="http://www.jostens.com/apps/shop/images/product/pchmp/FAN_RING1_GB030/browse.jpg"
/>
<img class="hover"
height="297"
width="331"
src="images/clear.gif"
data-lazyimages-src="http://www.jostens.com/apps/shop/images/product/pchmp/FAN_RING1_GB030/browse_hover.jpg"
/>
-
Interpreted language.
* But interpreters are getting faster.
** Not all browsers are equal.
-
Some statements are syntactically equivalent but
computationally different.
[ Example ]
-
1k is ok
-
String concatenation
[ Concat example ]
* New browsers don't need this
-
Reverse order
* New browsers don't need this
-
HTML: Incremental parsing vs. block parsing.
[ Parsing example ]
-
Careful with the call stack
* Too much recursion
( Need example)
-
"Mind what you have learned. Save you it can." - Yoda
[ Memory Example ]
* New browsers don't need this
-
A in AJAX?
-
Throttling
[ Throttle/debounce plugin ]
Works well for single events (keypress).
Does not work cross-event (keypress + blur).
Modified version in Enterprise Admin to throttle across event types.
-
Echo matching
Concept used in datatables jquery plugin.
[ Code ]
-
JSON size
You can't always get what you want... but sometimes you get more than you need.
-
Progress bar (progress.js in content lib).
[ Code ]
- Tells them what you're doing.
- Prevents them from doing something else.
Enterprise Admin Echo Code
var echo = dateUtil.ms();
asset.buildCustomAssetList.lastEcho = echo;
$.ajax({
url: asset.assetListUrl,
dataType: 'json',
data: {
"customerKey": customerId,
"echo": echo
},
complete: function() {
progress.hide();
},
success: function(data) {
if (ajaxUtil.isTimeout(data)) {
ajaxUtil.doSignin();
return;
}
if (asset.buildCustomAssetList.lastEcho != data.echo) {
return;
}
...
ShineFx Progress Bar Code
// show the pbar
progress.show({msg: "Initializing...", w: 400});
...
// just update the message
progress.msg("Preparing layouts...");
...
// hide the pbar
progress.hide();