87
Nano? Pico? Femto? Atto? Zepto! @thomasfuchs (cc) 2011 Thomas Fuchs

Zepto and the rise of the JavaScript Micro-Frameworks

Embed Size (px)

DESCRIPTION

Here are my slides from JSConf 2011 in Portland, Oregon. I was talking about Zepto, my micro-framework, WebKit-only (works on Firefox too, though) clone of the jQuery API, and why I think that Micro-Frameworks and Micro-Libraries are better suited for the mobile Web than traditional, monolithic libraries, like script.aculo.us, Prototype or jQuery. (A video of the talk is forthcoming and will be announced on my blog, http://mir.aculo.us).

Citation preview

Page 1: Zepto and the rise of the JavaScript Micro-Frameworks

Nano? Pico? Femto? Atto?

Zepto!@thomasfuchs

(cc) 2011 Thomas Fuchs

Page 2: Zepto and the rise of the JavaScript Micro-Frameworks

“real” computer

Page 3: Zepto and the rise of the JavaScript Micro-Frameworks

Fast and stable network connection

Lots of storage

Fast, multi-core CPUs

Hardware-accelerated graphics

Page 4: Zepto and the rise of the JavaScript Micro-Frameworks
Page 5: Zepto and the rise of the JavaScript Micro-Frameworks

Slow & unstable network connection

Limited storage

Slow CPUs

Hardware acceleration only on iOS

Page 6: Zepto and the rise of the JavaScript Micro-Frameworks

All major JS libs where created

before phones had web browsers to

write home about.

Page 7: Zepto and the rise of the JavaScript Micro-Frameworks
Page 8: Zepto and the rise of the JavaScript Micro-Frameworks

Don’t use something because it’s popular.

Use stuff because it’s the right tool for the job.

Page 9: Zepto and the rise of the JavaScript Micro-Frameworks
Page 10: Zepto and the rise of the JavaScript Micro-Frameworks

WebKit

Page 11: Zepto and the rise of the JavaScript Micro-Frameworks

Proprietary features

Page 12: Zepto and the rise of the JavaScript Micro-Frameworks

Adoption of features from JavaScript

frameworks

Page 13: Zepto and the rise of the JavaScript Micro-Frameworks

Proprietary featuresare awesome

Page 14: Zepto and the rise of the JavaScript Micro-Frameworks

CSS Selectors

Page 15: Zepto and the rise of the JavaScript Micro-Frameworks

document.querySelectorAll('div.awesome > p')

Page 16: Zepto and the rise of the JavaScript Micro-Frameworks

full featured CSS3 selectors

// select all li elements with both "just" and "testing" classnamesdocument.querySelectorAll('li.just.testing')

// how many paragraphs?document.querySelectorAll('p').length // select even paragraphsdocument.querySelectorAll('p:nth-child(2n+1)')

Page 17: Zepto and the rise of the JavaScript Micro-Frameworks

document.querySelectorAll returns a NodeList, not an array

[].slice.apply(nodelist)

convert to JavaScript array

Page 18: Zepto and the rise of the JavaScript Micro-Frameworks

querySelectorAll

Fast, native implementation

Returns a NodeList, not an array

Full-featured CSS selectors

No need for external JavaScript libraries

Page 19: Zepto and the rise of the JavaScript Micro-Frameworks

JSON

Page 20: Zepto and the rise of the JavaScript Micro-Frameworks

JSON.stringify({ s: 'a string', n: 123, d: new Date })

Page 21: Zepto and the rise of the JavaScript Micro-Frameworks

JSON.parse('{"some":"json","test":123}')

Page 22: Zepto and the rise of the JavaScript Micro-Frameworks

Native JSON

Fast, native implementation

Parsing JSON (convert to JS object)

Serializing JS objects to JSON

No problem with security of “eval” as in some JavaScript-based implementations

Page 23: Zepto and the rise of the JavaScript Micro-Frameworks

Array iteration

Page 24: Zepto and the rise of the JavaScript Micro-Frameworks

[1,2,3].forEach(alert);

Page 25: Zepto and the rise of the JavaScript Micro-Frameworks

[1,2,3].forEach(alert);

array with three numbers

Page 26: Zepto and the rise of the JavaScript Micro-Frameworks

[1,2,3].forEach(alert);

forEach is a native function on arrays, taking a function argument

Page 27: Zepto and the rise of the JavaScript Micro-Frameworks

[1,2,3].forEach(alert);

call with window.alert function

Page 28: Zepto and the rise of the JavaScript Micro-Frameworks

[1,2,3].forEach(alert);

Page 29: Zepto and the rise of the JavaScript Micro-Frameworks

Iterate through all elements found,alerting the element’s contents

[].slice.apply(nodelist).forEach( function(element){ alert(element.innerHTML); });

Page 30: Zepto and the rise of the JavaScript Micro-Frameworks

Array Iteration

Page 31: Zepto and the rise of the JavaScript Micro-Frameworks

No more for loops required

Array Iteration

Page 32: Zepto and the rise of the JavaScript Micro-Frameworks

No more for loops required

Array Iteration

No more for loops required

Page 33: Zepto and the rise of the JavaScript Micro-Frameworks

No more for loops required

Array Iteration

No more for loops required

No more for loops required

Page 34: Zepto and the rise of the JavaScript Micro-Frameworks

No more for loops required

Array Iteration

No more for loops required

No more for loops required

No more for loops required

Page 35: Zepto and the rise of the JavaScript Micro-Frameworks

No more for loops required

Array Iteration

No more for loops required

No more for loops required

No more for loops required

No more for loops required

Page 36: Zepto and the rise of the JavaScript Micro-Frameworks

Mobile JavaScript framework?

Page 37: Zepto and the rise of the JavaScript Micro-Frameworks

Why not use Prototype, jQuery or other frameworks?

Page 38: Zepto and the rise of the JavaScript Micro-Frameworks

Some functionality is not supported or not meaningful

on mobile devices.

resizing & scrollingorientationfixed positioningfontsSVG

Page 39: Zepto and the rise of the JavaScript Micro-Frameworks

More code causes longer download and initialization times.

Page 40: Zepto and the rise of the JavaScript Micro-Frameworks

Most of the downloaded code isn’t even used.

(there’s no IE 6 to support on mobile phones, lucky us)

Page 41: Zepto and the rise of the JavaScript Micro-Frameworks

A lot of the rest of the code is duplicating features that are directly available as native

implementations.

Page 42: Zepto and the rise of the JavaScript Micro-Frameworks

Goals for a mobile JavaScript framework

Page 43: Zepto and the rise of the JavaScript Micro-Frameworks

Reduce code size as much as possible to keep download and

initialization times down.

Page 44: Zepto and the rise of the JavaScript Micro-Frameworks

Easy to use API—possibly emulating jQuery because

developers already know it.

Page 45: Zepto and the rise of the JavaScript Micro-Frameworks

Easy to extend and customize—again, jQuery has a familiar plugin/

extension mechanism

Page 46: Zepto and the rise of the JavaScript Micro-Frameworks

Ideally, have a fallback mechanismin case it’s run on non-WebKit

mobile browsers.

Page 47: Zepto and the rise of the JavaScript Micro-Frameworks

It’s not so important what’s there, but what’s not there.

Page 48: Zepto and the rise of the JavaScript Micro-Frameworks

Meet zepto.jshttp://github.com/madrobby/zepto

Target size: 5K

jQuery-compatible API

Uses mobile WebKit features whenever possible

Easily replaceable with jQuery proper if needed

Doesn’t cover all of jQuery (but lots of it!)

Page 49: Zepto and the rise of the JavaScript Micro-Frameworks

31.33K

4.83K

jQuery 1.6 Zepto (master)

Page 50: Zepto and the rise of the JavaScript Micro-Frameworks
Page 51: Zepto and the rise of the JavaScript Micro-Frameworks

Various special cases

Page 52: Zepto and the rise of the JavaScript Micro-Frameworks

Main use case $(some selector)

Page 53: Zepto and the rise of the JavaScript Micro-Frameworks

this saves ~6k of selector engine code

Page 54: Zepto and the rise of the JavaScript Micro-Frameworks
Page 55: Zepto and the rise of the JavaScript Micro-Frameworks

make sure dom is a JavaScript array

Page 56: Zepto and the rise of the JavaScript Micro-Frameworks

swap out the prototype,

but leave “length” and other properties

intact, uses the proprietary

__proto__ property

Page 57: Zepto and the rise of the JavaScript Micro-Frameworks

Z.prototype is pointing to $.fn which holds all methods that are used on found elements

Page 58: Zepto and the rise of the JavaScript Micro-Frameworks

Reusing array methods, works because we have an array-like object

Page 59: Zepto and the rise of the JavaScript Micro-Frameworks

this is an array-like of resulting nodes and a Zepto object at the same time

Page 60: Zepto and the rise of the JavaScript Micro-Frameworks

insertAdjacentElement is IE-proprietary, but supported by WebKit

(doesn’t work on Firefox!)

Page 61: Zepto and the rise of the JavaScript Micro-Frameworks

Zepto.jshttp://github.com/madrobby/zepto

CSS Selectors and DOM manipulation

Ajax including x-domain JSONP

Events (including touch events)

Polyfills and bug fixes for older WebKits

Modular (can leave out events, xhr, etc.)

WebKit only! (with focus on mobile)

Page 62: Zepto and the rise of the JavaScript Micro-Frameworks
Page 63: Zepto and the rise of the JavaScript Micro-Frameworks
Page 64: Zepto and the rise of the JavaScript Micro-Frameworks

/

Page 65: Zepto and the rise of the JavaScript Micro-Frameworks

m.checkers.com

Page 66: Zepto and the rise of the JavaScript Micro-Frameworks
Page 67: Zepto and the rise of the JavaScript Micro-Frameworks
Page 68: Zepto and the rise of the JavaScript Micro-Frameworks

One more thing…

Page 69: Zepto and the rise of the JavaScript Micro-Frameworks

scriptaculous

Page 70: Zepto and the rise of the JavaScript Micro-Frameworks

Prototype.js

Page 71: Zepto and the rise of the JavaScript Micro-Frameworks

jQuery

Page 72: Zepto and the rise of the JavaScript Micro-Frameworks

mootools

Page 73: Zepto and the rise of the JavaScript Micro-Frameworks

you/users are the rebels—the ewoks are helping you achieve your goals

Micro-Frameworks

Page 74: Zepto and the rise of the JavaScript Micro-Frameworks
Page 75: Zepto and the rise of the JavaScript Micro-Frameworks

Classic frameworks

25k+ minified & gzipped

Do all things and do it ok-ish

Many extensions are now availablein the DOM or JavaScript

Force you into an API

Not modular/not modular enough

Page 76: Zepto and the rise of the JavaScript Micro-Frameworks

Micro-Frameworks(are awesome!)

Page 77: Zepto and the rise of the JavaScript Micro-Frameworks

do one thing and do it really well

Page 78: Zepto and the rise of the JavaScript Micro-Frameworks

smaller than 5k, minified & gzipped

Page 79: Zepto and the rise of the JavaScript Micro-Frameworks

use directly or loosely coupled

Page 80: Zepto and the rise of the JavaScript Micro-Frameworks

Small is beautiful

downloads and runs faster

easier to understand code & fork

fewer bugs (less code!)

…and you’ll learn how JavaScript REALLY works

Page 81: Zepto and the rise of the JavaScript Micro-Frameworks

{{ mustache }} ~ 1.5k

Page 82: Zepto and the rise of the JavaScript Micro-Frameworks

Lawnchair ~ 2.0k

Page 83: Zepto and the rise of the JavaScript Micro-Frameworks

Backbone.js ~ 3.9k

Page 84: Zepto and the rise of the JavaScript Micro-Frameworks

But how do I know what’s out there?

Page 85: Zepto and the rise of the JavaScript Micro-Frameworks

microjs.com

Page 86: Zepto and the rise of the JavaScript Micro-Frameworks

github.com/madrobby/microjs.com

Add your own!

Page 87: Zepto and the rise of the JavaScript Micro-Frameworks

Questions?@thomasfuchs