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

Efficient JavaScript

volkan
March 30, 2013

Efficient JavaScript

This was a 3-hour workshop that I gave at udemy.com headquarters, about JavaScript application development best practices.

volkan

March 30, 2013
Tweet

More Decks by volkan

Other Decks in Technology

Transcript

  1. Outline • Overview: Front-End Development Best Practices • Useful JavaScript

    Patterns & Practices • A ReIntroduction to JavaScript Memory Leaks • Profiling JavaScript Applications • Practice Time: Step by Step Optimizing a Sample Application • Q&A Thursday, October 3, 13
  2. Best Practices (micro) • eval is evil • Avoid try/catch

    • Avoid with • Avoid $.each and for/in loops • Use String Builders • Primitive operations are faster than function calls Thursday, October 3, 13
  3. Best Practices (Speed & Memory) • Minimize DOM Size and

    Depth • Minimize DOM Repaints and Reflows (cache DOM calculations) • Keep Your Markup Clean (avoid inline & embedded code) • Purge Anything that is not Visible (from DOM AND from Memory) • Async Load Third-Party Widgets • Smoothness/Speed Tradeoff Thursday, October 3, 13
  4. Best Practices (Perceived Speed) • Load Lazily; Execute Lazily •

    Do Not Wait for Server Feedback • Styles to the Top, Scripts to the Bottom Thursday, October 3, 13
  5. Best Practices (Transfer Speed) • Far Future Expires Header •

    Minify Your Assets • Combine Multiple Assets • CSS Sprites, Image Data URIs, Image Optimization • Cache (client-side and server-side) & Gzip Thursday, October 3, 13
  6. Best Practices (Macro) • Make JavaScript and CSS External •

    Make AJAX Cacheable • Use a Strict REST Architecture • Reduce Cookie Size • Use Cookie-Free Domains for Components • Make favicon.ico Small and Cacheable Thursday, October 3, 13
  7. Best Practices (Development) • Utilize Diagnostic Tools (YSlow, Chrome Dev

    Tools) •Know Thy Frameworks • Live in TDD (jasmine, jshint, istanbul) • One Click Publish, Automation, and CI (git pre-commit hooks, grunt, jenkins) Thursday, October 3, 13
  8. Patterns & Practices (Building Blocks) • Prototypal Inheritance • Model-View-Whatever

    (MVC, MVP, MVVM…) • Module — / AMD • Continuation Passing Style • Mixin / Decorator / Dependency Injection • Façade / PubSub — / Mediator • Flow Control / Deferreds / Promises — Thursday, October 3, 13
  9. Patterns & Practices (More) • Chaining • Factory • Composite

    • Strategy • Command / Chain of Responsibility • Bridge • Flyweight • Iterator • Null Object • Singleton • and the list goes on… Thursday, October 3, 13
  10. Think Simple • E = MC^2 • Simplicity is the

    Ultimate Sophistication • When in doubt, KISS. Thursday, October 3, 13
  11. Memory Leaks (Overview) • How JavaScript Garbage Collector Works •

    Memory Leaks in Older Browsers (read: Internet Explorer) • Memory Leaks Related to Timers • Memory Leaks Related to Debug Console • Size Does Matter • Structural Leaks Thursday, October 3, 13
  12. Structural Leaks • These Are NOT Because of the Browser

    Bugs • They Are the Most Common Leaks • Handlers, Registries, Controllers, Managers, Mediators,Observables Are Leaker Candidates • Leaks are in the Nature of Event-Based Architectures • Closures and Modules Are Controlled Memory Leaks • Good kids clean their mess! Thursday, October 3, 13
  13. Garbage-Collector- Friendly Code • Use Variables With a Proper Scope

    • Unbind Events • Clear Timers • Invalidate Cache Thursday, October 3, 13
  14. Agenda • Creating a Logical Folder Structure • Configuring the

    Builder, Linter, and Tests (grunt, jshint, jasmine, istanbul) • Profiling the Application & Fixing Leaks • Localizing the Application • Decoupling Modules Using Custom Events (PubSubHub) • Adding a Mediator (PubSub and Hub) • Implementing DOM Event Delegation • Improving Render Performance • Code Coverage Analysis • You Need Testing Thursday, October 3, 13
  15. Unit Testing • Test State (NOT User Interface, NOT Interactions)

    • Assemble • Act • Assert Thursday, October 3, 13