trimming the cruft

49
Trimming The Cruft: Getting the most of your bits Peter Higgins (dante) Dojo Toolkit Project Lead DevNexus: March 10 + 11, 2009

Upload: peter-higgins

Post on 09-May-2015

1.309 views

Category:

Technology


0 download

DESCRIPTION

A Browser performance optimization talk which includes bits on Dojo's automated steps to alleviate much pain.

TRANSCRIPT

Page 1: Trimming The Cruft

Trimming The Cruft:Getting the most of your bits

Peter Higgins (dante)Dojo Toolkit Project Lead

DevNexus: March 10 + 11, 2009

Page 2: Trimming The Cruft

Me.

Page 3: Trimming The Cruft
Page 4: Trimming The Cruft

What is Dojo?

• A JavaScript Toolkit- Light-weight Base. - Use at will framework.

• 100 - point Open Source- Clean IP - Open Cycle

• Developer Tools

Page 5: Trimming The Cruft

The History of Dojo(cli!notes version)

Page 6: Trimming The Cruft

Alex Russell“netwindows”

Page 7: Trimming The Cruft

A Foundation.

Page 8: Trimming The Cruft

Unifying a DHTML landscape

Page 9: Trimming The Cruft

A Team.

Page 10: Trimming The Cruft

Maintaining a vision.

Page 11: Trimming The Cruft

Dojo, Brie"y.

Page 12: Trimming The Cruft

Plain JavaScript

• JavaScript should have:- dojo.indexOf / forEach / map / #lter - hitch / partial - declare / delegate / mixin / extend

Page 13: Trimming The Cruft

Package / Module Loading

• dojo.require(“some.module”)• dojo.addOnLoad(function(){ ... })• dojo.registerModulePath(“my”, “../../my/”)• dojo.require(“my.code”)• dojo.provide(“my.code”)

Page 14: Trimming The Cruft

Events, Ajax, FX, DOM

• dojo.connect(node, “onclick”, function(){ });• dojo.connect(obj, “custom”, obj, “sync”);• dojo.xhrPost({ form:”login”, load:function(dat){}});• dojo.xhrGet({ url:”foo.txt”, load:function(){ } });• dojo.anim(node, { width:200 });• dojo.fadeIn({ node: node }).play();• dojo.style(n, “height”, “50px”);• dojo.place(“<p>Hi</p>”, dojo.body());

Page 15: Trimming The Cruft

CSS Selectors

• dojo.query(“ul > li”).addClass(“listy”);• dojo.query(“a”).onclick(function(){ });• dojo.query(“tr td + td”).forEach(function(n){ });• dojo.query(“.someClass”).map(function(n){ .. });

• dojo.NodeList.prototype / dojo.extend

Page 16: Trimming The Cruft

Flexible Dynamic Language

// confuse people(function(foo){ var f = foo, fid = f.byId;

f.require(“my.code”); f.addOnLoad(function(){

new my.Thinger({ node: fid(‘bar’) }); });})(dojo);

// bling(function(d, $){ $(“.class”).onclick(function(){ });})(dojo, dojo.query);

Page 17: Trimming The Cruft

Optimization Fundamentals

Page 18: Trimming The Cruft

Separate Assets.

Page 19: Trimming The Cruft

Scripts are Blocking, Synchronous.

Page 20: Trimming The Cruft

<head> <script src=”dojo.js”></script> <script>/* code in dojo.js ready */</script></head><body><p>Lots of DOM</p></body>

<body> <p>Lots of DOM</p> <script src=”dojo.js”></script> <script>/* code in dojo.js ready */</script></body>

vs:

Page 21: Trimming The Cruft

Multiple Inline Scripts

Page 22: Trimming The Cruft

Know request limits.

Page 23: Trimming The Cruft

gzip “required”.

• Incredible, exponential savings.- Bene#ts from patterns

• 9 / 10 Major Browsers Support

Page 24: Trimming The Cruft

CSS is “async”

• No reliable “onload”• Put requests #rst• Limit @import

Page 25: Trimming The Cruft

De#ne “ready”

• DOMContentLoaded & window.onload• CSS, JS and Image resources

- required or deferred?- preloaded and cached.

Page 26: Trimming The Cruft

Content Distribution (CDN)

Page 27: Trimming The Cruft

CDN Bene#ts

• Shared Resources- Cached, common, static

• Public vs Private• Headers / Cookies• Beating request limits

Page 28: Trimming The Cruft

Client Considerations(on the wire cost)

Page 29: Trimming The Cruft

JSON vs XML

Page 30: Trimming The Cruft

JavaScript is Fast.

Page 31: Trimming The Cruft

The DOM isn’t.

Page 32: Trimming The Cruft

DOM & CSS

• Size• Complexity• Manipulation

- Avoiding loops- working outside the DOM

• Server vs Client

Page 33: Trimming The Cruft

Events

• Delegation- Normalization

- Bubbles, Cancels• Invention

- mouseenter/mouseleave- onDijitClick

Page 34: Trimming The Cruft

Code Considerations(also on the client)

Page 35: Trimming The Cruft

Modularity. Inheritance.

Page 36: Trimming The Cruft

More on Packages

// the package:dojo.provide(“my.Module”); // my/Module.jsdojo.require(“my._ClassBase”); // my/_ClassBase.jsdojo.declare(“my.Module”, my._ClassBase, {});

// the code:dojo.require(“my.Module”);dojo.addOnLoad(function(){ new my.Module();});

Page 37: Trimming The Cruft

Mix-in Patterns

Page 38: Trimming The Cruft

Arguments, etc.

// function signatures with defaultsmy.func = function(args){ var opts = dojo.mixin({ prop:”foo” }, args||{}); console.log(opts.prop);}

my.func(); // “foo”my.func({ prop:”bar” }); // “bar”

Page 39: Trimming The Cruft

Ambiguous Constructors

dojo.declare(“some.Thing”, null, { opt:”a-default”, constructor: function(args){

dojo.mixin(this, args); }});

var a = new some.Thing(), b = new some.Thing({ opt:”x” });a.opt == “a-default”b.opt == “x”

Page 40: Trimming The Cruft

DRY

my.turnIt = function(dir){// if 1, go next. -1, go left.

}

var next = dojo.partial(my.turnIt, 1), prev = dojo.partial(my.turnIt, -1);

// laterdojo.query(“.nextbutton”).onclick(next);

Page 41: Trimming The Cruft

Dual Service Functions

my.func = function(node, args){ node = dojo.byId(node); // Sting|DomNode /* do something to node */}

dojo.extend(dojo.NodeList, { func: dojo.NodeList._mapAsForEach(“func”, my)});

// run one, run allmy.func(“someId”, { opts:true });dojo.query(“.someNodes”).func({ opts:true })

Page 42: Trimming The Cruft

Mini#cation

Page 43: Trimming The Cruft

To consider:

• YUI / ShrinkSafe / jsmin / packer- variables, whitespace- preserve public APIs

• Micro vs. Premature optimizations- For GZIP- For ShrinkSafe

Page 44: Trimming The Cruft

Dojo Build System

Page 45: Trimming The Cruft

All-in-One

• Works transparently with Package System• Group modules into layers• Concatenate CSS into layers• Layer & File mini#cation

- Comments, Whitespace, newlines ...

Page 46: Trimming The Cruft

Special Builds

• Stubs (6k dojo.js)• Base++ (dojo.js with modules)• Cross-Domain• WebKit Mobile• plugd• Maven

Page 47: Trimming The Cruft

Build Options

• #ifDef - WebKit Mobile

• stripConsole• internStrings

Page 48: Trimming The Cruft

Questions?