Zepto and the rise of the JavaScript Micro-Frameworks

JavaScript Guru at Slash7
May. 6, 2011
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
Zepto and the rise of the JavaScript Micro-Frameworks
1 of 87

More Related Content

Zepto and the rise of the JavaScript Micro-Frameworks

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

  2. “real” computer

  3. Fast and stable network connection Lots of storage Fast, multi-core CPUs Hardware-accelerated graphics

  4. Slow & unstable network connection Limited storage Slow CPUs Hardware acceleration only on iOS

  5. All major JS libs where created before phones had web browsers to write home about.

  6. Don’t use something because it’s popular. Use stuff because it’s the right tool for the job.

  7. WebKit

  8. Proprietary features

  9. Adoption of features from JavaScript frameworks

  10. Proprietary features are awesome

  11. CSS Selectors

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

  13. full featured CSS3 selectors // select all li elements with both "just" and "testing" classnames document.querySelectorAll('li.just.testing') // how many paragraphs? document.querySelectorAll('p').length // select even paragraphs document.querySelectorAll('p:nth-child(2n+1)')

  14. [].slice.apply(nodelist) convert to JavaScript array document.querySelectorAll returns a NodeList, not an array

  15. querySelectorAll Full-featured CSS selectors No need for external JavaScript libraries Fast, native implementation Returns a NodeList, not an array

  16. JSON

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

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

  19. Native JSON Parsing JSON (convert to JS object) Serializing JS objects to JSON Fast, native implementation No problem with security of “eval” as in some JavaScript-based implementations

  20. Array iteration

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

  22. [1,2,3].forEach(alert); array with three numbers

  23. [1,2,3].forEach(alert); forEach is a native function on arrays, taking a function argument

  24. [1,2,3].forEach(alert); call with window.alert function

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

  26. [].slice.apply(nodelist).forEach( function(element){ alert(element.innerHTML); } ); Iterate through all elements found, alerting the element’s contents

  27. Array Iteration

  28. Array Iteration No more for loops required

  29. Array Iteration No more for loops required No more for loops required

  30. Array Iteration No more for loops required No more for loops required No more for loops required

  31. Array Iteration No more for loops required No more for loops required No more for loops required No more for loops required

  32. Array Iteration No more for loops required No more for loops required No more for loops required No more for loops required No more for loops required

  33. Mobile JavaScript framework?

  34. Why not use Prototype, jQuery or other frameworks?

  35. Some functionality is not supported or not meaningful on mobile devices. resizing & scrolling orientation fixed positioning fonts SVG

  36. More code causes longer download and initialization times.

  37. Most of the downloaded code isn’t even used. (there’s no IE 6 to support on mobile phones, lucky us)

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

  39. Goals for a mobile JavaScript framework

  40. Reduce code size as much as possible to keep download and initialization times down.

  41. Easy to use API—possibly emulating jQuery because developers already know it.

  42. Easy to extend and customize— again, jQuery has a familiar plugin/ extension mechanism

  43. Ideally, have a fallback mechanism in case it’s run on non-WebKit mobile browsers.

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

  45. Meet zepto.js http://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!)

  46. 31.33K 4.83K Zepto (master) jQuery 1.6

  47. Various special cases

  48. Main use case $(some selector)

  49. this saves ~6k of selector engine code

  50. make sure dom is a JavaScript array

  51. swap out the prototype, but leave “length” and other properties intact, uses the proprietary __proto__ property

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

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

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

  55. insertAdjacentElement is IE- proprietary, but supported by WebKit (doesn’t work on Firefox!)

  56. Zepto.js http://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)

  57. /

  58. m.checkers.com

  59. One more thing…

  60. scriptaculous

  61. Prototype.js

  62. jQuery

  63. mootools

  64. Micro-Frameworks you/users are the rebels —the ewoks are helping you achieve your goals

  65. Classic frameworks Do all things and do it ok-ish Force you into an API Not modular/not modular enough 25k+ minified & gzipped Many extensions are now available in the DOM or JavaScript

  66. Micro-Frameworks (are awesome!)

  67. do one thing and do it really well

  68. smaller than 5k, minified & gzipped

  69. use directly or loosely coupled

  70. Small is beautiful easier to understand code & fork downloads and runs faster fewer bugs (less code!) …and you’ll learn how JavaScript REALLY works

  71. {{ mustache }} ~ 1.5k

  72. Lawnchair ~ 2.0k

  73. Backbone.js ~ 3.9k

  74. But how do I know what’s out there?

  75. microjs.com

  76. Add your own! github.com/madrobby/ microjs.com

  77. Questions? @thomasfuchs