Upgrade to Pro — share decks privately, control downloads, hide ads and more …

When Not to Use YUI

Ryan Grove
November 15, 2012

When Not to Use YUI

Video: http://youtu.be/8cTz73zdDuc?hd=1

In this talk from YUIConf 2012, I provide code samples and real-world anecdotes to illustrate how to decide when to use YUI, when to use vanilla JavaScript, when to consider other libraries, and what the tradeoffs are in terms of performance and maintainability. Advice ranges from simple rules of thumb to more nuanced discussion of complex architectural decisions, with examples drawn from my time working on YUI at Yahoo! and using YUI at SmugMug.

Ryan Grove

November 15, 2012
Tweet

More Decks by Ryan Grove

Other Decks in Programming

Transcript

  1. kittens movies with happy endings pie awesomeness JavaScript movies with

    sad endings the ocean not giving talks YUI driving really fast gadgets old-ass sailing ships coffee unit tests sleeping in Pocky thunderstorms
  2. kittens movies with happy endings pie awesomeness JavaScript movies with

    sad endings the ocean not giving talks driving really fast old-ass sailing ships gadgets coffee unit tests sleeping in Pocky thunderstorms YUI
  3. Don’t use a tool just because you have a tool

    http://www.flickr.com/photos/carlwain/3555650607/
  4. Y.Lang.isBoolean() Chrome 22 IE 9 Firefox 16 iOS 6 1%

    slower 45% slower 9% slower 5% slower Compared to typeof value === ‘boolean’ http://jsperf.com/y-lang-isboolean AVOID Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2 and an iPhone 4S, iOS 6.0
  5. Y.Lang.isFunction() Chrome 22 IE 9 Firefox 16 iOS 6 58%

    slower 68% slower 64% slower 75% slower Compared to typeof value === ‘function’ http://jsperf.com/y-lang-isfunction AVOID Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2 and an iPhone 4S, iOS 6.0
  6. Y.Lang.isNull() Chrome 22 IE 9 Firefox 16 iOS 6 3%

    slower 48% slower 10% slower 3% slower Compared to value === null http://jsperf.com/y-lang-isnull AVOID Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2 and an iPhone 4S, iOS 6.0
  7. Y.Lang.isString() Chrome 22 IE 9 Firefox 16 iOS 6 1%

    slower 49% slower 8% slower 6% slower Compared to typeof value === ‘string’ http://jsperf.com/y-lang-isstring AVOID Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2 and an iPhone 4S, iOS 6.0
  8. Chrome 22 IE 9 Firefox 16 iOS 6 0.4% slower

    47% slower 7% slower 5% slower Y.Lang.isUndefined() Compared to typeof value === ‘undefined’ http://jsperf.com/y-lang-isundefined AVOID Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2 and an iPhone 4S, iOS 6.0
  9. Chrome 22 IE 9 Firefox 16 iOS 6 isBoolean() isFunction()

    isNull() isString() isUndefined() 1% slower 45% slower 9% slower 5% slower 58% slower 68% slower 64% slower 75% slower 3% slower 48% slower 10% slower 3% slower 1% slower 49% slower 8% slower 6% slower 0.4% slower 47% slower 7% slower 5% slower
  10. Y.instanceOf() Chrome 22 IE 9 Firefox 16 iOS 6 26%

    slower 58% slower 53% slower 49% slower Compared to the instanceof operator http://jsperf.com/y-instanceof AVOID Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2 and an iPhone 4S, iOS 6.0
  11. Chrome 22 IE 9 Firefox 16 iOS 6 99.7% slower

    99.6% slower 99.9% slower 99.8% slower Y.Base vs. vanilla JS Comparing instantiation speed http://jsperf.com/y-base caution Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2 and an iPhone 4S, iOS 6.0
  12. Y.Base vs. vanilla JS Comparing instantiation speed (ops/sec) http://jsperf.com/y-base caution

    Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2 and an iPhone 4S, iOS 6.0 Chrome 22 IE 9 Firefox 16 iOS 6 Y.Base vanilla JS 10,924 2,323 6,196 1,717 4,013,182 544,036 8,189,437 713,684
  13. caution •~2,040 bytes per Y.Base instance •1,000 Y.Base instances: ~1.9MB

    •~12 bytes per vanilla JS instance •1,000 vanilla instances: ~11.7KB Y.Base memory usage
  14. Y.Anim Y.App Y.AutoComplete Y.Button Y.Cache Y.Calendar Y.DataSource.* Y.DataTable Y.DD.* Y.Dial

    Y.EditorBase Y.LazyModelList Y.Model Y.ModelList Y.Overlay Y.Panel Y.Pjax Y.Record Y.Recordset Y.Resize Y.Router Y.ScrollView Y.Slider Y.Sortable Y.Tab Y.TabView Y.ToggleButton Y.Uploader Y.View Y.Widget Y.WidgetParent Y.WidgetChild
  15. My God, it’s full of models and views 2001: A

    Space Odyssey (1968), MGM Studios
  16. Model + View Model + View Model + View Model

    + View Model + View Model + View Model + View Model + View Model + View Model + View Model + View Model + View
  17. •Throttled scroll event handlers •Efficiently-computed scroll metrics •Convenient events for

    actions like scrollToBottom and scrollToTop Y.Plugin.ScrollInfo http://yuilibrary.com/yui/docs/api/classes/Plugin.ScrollInfo.html
  18. • Same API as ModelList • Contains vanilla JavaScript objects

    instead of Model instances • revive() and free() Model instances as needed Y.LazyModelList http://yuilibrary.com/yui/docs/model-list/#lazymodellist
  19. SmugMug Search benchmarks Before After Time to initial results Initial

    memory usage Time to manually scroll to 1,000 results Memory usage after 1,000 results 1,410ms 950ms (33% faster) 8.26MB 7.23MB (12% less) 30s 22s (27% faster) 42.82MB 15.64MB (63% less)
  20. Y.Handlebars Underscore Y.Template.Micro Render Compile & Render 146,446 ops/s 197,123

    ops/s 205,716 ops/s 4,501 ops/s 28,966 ops/s 36,737 ops/s Y.Template.Micro vs. others http://jsperf.com/y-template-vs-others/6 Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2
  21. •Native ECMAScript 5 feature •Supported by all modern browsers •Getters/setters

    •Read-only properties •Super cheap vs. Y.Attribute Object.defineProperty()
  22. Y.Property vs. Y.Attribute Comparing instantiation speed (ops/sec) of base classes

    http://jsperf.com/y-property-vs-y-attribute/4 Benchmarked on a Mac Pro, 2.8GHz quad-core Xeon, 16 GB RAM, OS X 10.8.2 and an iPhone 4S, iOS 6.0 Chrome 22 IE 9 Firefox 16 iOS 6 Y.Attribute Y.Property 22,919 7,140 15,669 4,794 56,222 145% faster 48,276 576% faster 110,163 603% faster 28,508 495% faster
  23. Coming soon to a YUI near you? Maybe. Until then:

    https://github.com/smugmug/yui3
  24. YUI({ groups: { 'cdnjs': { base: '//cdnjs.cloudflare.com/ajax/libs', modules: { xregexp:

    { path: '/xregexp/2.0.0/xregexp-min.js' } } } }, onProgress: function (e) { if (e.data[0].name === 'xregexp') { YUI.add('xregexp', function (Y) { Y.XRegExp = XRegExp; }); } } }).use('xregexp', function (Y) { console.log('xregexp is now available at Y.XRegExp'); });
  25. YUI({ groups: { 'cdnjs': { base: '//cdnjs.cloudflare.com/ajax/libs', modules: { xregexp:

    { path: '/xregexp/2.0.0/xregexp-min.js' } } } }, onProgress: function (e) { if (e.data[0].name === 'xregexp') { YUI.add('xregexp', function (Y) { Y.XRegExp = XRegExp; }); } } }).use('xregexp', function (Y) { console.log('xregexp is now available at Y.XRegExp'); });
  26. YUI({ groups: { 'cdnjs': { base: '//cdnjs.cloudflare.com/ajax/libs', modules: { xregexp:

    { path: '/xregexp/2.0.0/xregexp-min.js' } } } }, onProgress: function (e) { if (e.data[0].name === 'xregexp') { YUI.add('xregexp', function (Y) { Y.XRegExp = XRegExp; }); } } }).use('xregexp', function (Y) { console.log('xregexp is now available at Y.XRegExp'); });
  27. YUI({ groups: { 'cdnjs': { base: '//cdnjs.cloudflare.com/ajax/libs', modules: { xregexp:

    { path: '/xregexp/2.0.0/xregexp-min.js' } } } }, onProgress: function (e) { if (e.data[0].name === 'xregexp') { YUI.add('xregexp', function (Y) { Y.XRegExp = XRegExp; }); } } }).use('xregexp', function (Y) { console.log('xregexp is now available at Y.XRegExp'); });
  28. YUI({ groups: { 'cdnjs': { base: '//cdnjs.cloudflare.com/ajax/libs', modules: { xregexp:

    { path: '/xregexp/2.0.0/xregexp-min.js' } } } }, onProgress: function (e) { if (e.data[0].name === 'xregexp') { YUI.add('xregexp', function (Y) { Y.XRegExp = XRegExp; }); } } }).use('xregexp', function (Y) { console.log('xregexp is now available at Y.XRegExp'); });
  29. YUI({ groups: { 'cdnjs': { base: '//cdnjs.cloudflare.com/ajax/libs', modules: { xregexp:

    { path: '/xregexp/2.0.0/xregexp-min.js' } } } }, onProgress: function (e) { if (e.data[0].name === 'xregexp') { YUI.add('xregexp', function (Y) { Y.XRegExp = XRegExp; }); } } }).use('xregexp', function (Y) { console.log('xregexp is now available at Y.XRegExp'); });
  30. Thank you! @yaypie on Twitter and ADN rgrove on GitHub

    & Freenode https://speakerdeck.com/yaypie/when-not-to-use-yui