High Performance JavaScript

Book description

If you're like most developers, you rely heavily on JavaScript to build interactive and quick-responding web applications. The problem is that all of those lines of JavaScript code can slow down your apps. This book reveals techniques and strategies to help you eliminate performance bottlenecks during development. You'll learn how to improve execution time, downloading, interaction with the DOM, page life cycle, and more.

Yahoo! frontend engineer Nicholas C. Zakas and five other JavaScript experts—Ross Harmes, Julien Lecomte, Steven Levithan, Stoyan Stefanov, and Matt Sweeney—demonstrate optimal ways to load code onto a page, and offer programming tips to help your JavaScript run as efficiently and quickly as possible. You'll learn the best practices to build and deploy your files to a production environment, and tools that can help you find problems once your site goes live.

  • Identify problem code and use faster alternatives to accomplish the same task
  • Improve scripts by learning how JavaScript stores and accesses data
  • Implement JavaScript code so that it doesn't slow down interaction with the DOM
  • Use optimization techniques to improve runtime performance
  • Learn ways to ensure the UI is responsive at all times
  • Achieve faster client-server communication
  • Use a build system to minify files, and HTTP compression to deliver them to the browser

Publisher resources

View/Submit Errata

Table of contents

  1. Dedication
  2. Foreword
  3. Preface
    1. The Internet Evolves
    2. Why Optimization Is Necessary
    3. Next-Generation JavaScript Engines
    4. Performance Is Still a Concern
    5. How This Book Is Organized
    6. JavaScript Loading
    7. Coding Technique
    8. Deployment
    9. Testing
    10. Who This Book Is For
    11. Conventions Used in This Book
    12. Using Code Examples
    13. Safari® Books Online
    14. How to Contact Us
    15. Acknowledgments
  4. 1. Loading and Execution
    1. Script Positioning
    2. Grouping Scripts
    3. Nonblocking Scripts
      1. Deferred Scripts
      2. Dynamic Script Elements
      3. XMLHttpRequest Script Injection
      4. Recommended Nonblocking Pattern
        1. The YUI 3 approach
        2. The LazyLoad library
        3. The LABjs library
    4. Summary
  5. 2. Data Access
    1. Managing Scope
      1. Scope Chains and Identifier Resolution
      2. Identifier Resolution Performance
      3. Scope Chain Augmentation
      4. Dynamic Scopes
      5. Closures, Scope, and Memory
    2. Object Members
      1. Prototypes
      2. Prototype Chains
      3. Nested Members
      4. Caching Object Member Values
    3. Summary
  6. 3. DOM Scripting
    1. DOM in the Browser World
      1. Inherently Slow
    2. DOM Access and Modification
      1. innerHTML Versus DOM methods
      2. Cloning Nodes
      3. HTML Collections
        1. Expensive collections
        2. Local variables when accessing collection elements
      4. Walking the DOM
        1. Crawling the DOM
        2. Element nodes
        3. The Selectors API
    3. Repaints and Reflows
      1. When Does a Reflow Happen?
      2. Queuing and Flushing Render Tree Changes
      3. Minimizing Repaints and Reflows
        1. Style changes
        2. Batching DOM changes
      4. Caching Layout Information
      5. Take Elements Out of the Flow for Animations
      6. IE and :hover
    4. Event Delegation
    5. Summary
  7. 4. Algorithms and Flow Control
    1. Loops
      1. Types of Loops
      2. Loop Performance
        1. Decreasing the work per iteration
        2. Decreasing the number of iterations
      3. Function-Based Iteration
    2. Conditionals
      1. if-else Versus switch
      2. Optimizing if-else
      3. Lookup Tables
    3. Recursion
      1. Call Stack Limits
      2. Recursion Patterns
      3. Iteration
      4. Memoization
    4. Summary
  8. 5. Strings and Regular Expressions
    1. String Concatenation
      1. Plus (+) and Plus-Equals (+=) Operators
        1. Firefox and compile-time folding
      2. Array Joining
      3. String.prototype.concat
    2. Regular Expression Optimization
      1. How Regular Expressions Work
      2. Understanding Backtracking
        1. Alternation and backtracking
        2. Repetition and backtracking
      3. Runaway Backtracking
        1. The solution: Be specific
        2. Emulating atomic groups using lookahead and backreferences
        3. Nested quantifiers and runaway backtracking
          1. From bad to worse
      4. A Note on Benchmarking
      5. More Ways to Improve Regular Expression Efficiency
      6. When Not to Use Regular Expressions
    3. String Trimming
      1. Trimming with Regular Expressions
      2. Trimming Without Regular Expressions
      3. A Hybrid Solution
    4. Summary
  9. 6. Responsive Interfaces
    1. The Browser UI Thread
      1. Browser Limits
      2. How Long Is Too Long?
    2. Yielding with Timers
      1. Timer Basics
      2. Timer Precision
      3. Array Processing with Timers
      4. Splitting Up Tasks
      5. Timed Code
      6. Timers and Performance
    3. Web Workers
      1. Worker Environment
      2. Worker Communication
      3. Loading External Files
      4. Practical Uses
    4. Summary
  10. 7. Ajax
    1. Data Transmission
      1. Requesting Data
        1. XMLHttpRequest
          1. POST versus GET when using XHR
        2. Dynamic script tag insertion
        3. Multipart XHR
      2. Sending Data
        1. XMLHttpRequest
        2. Beacons
    2. Data Formats
      1. XML
        1. XPath
        2. Response sizes and parse times
      2. JSON
        1. JSON-P
        2. Should you use JSON?
      3. HTML
      4. Custom Formatting
      5. Data Format Conclusions
    3. Ajax Performance Guidelines
      1. Cache Data
        1. Setting HTTP headers
        2. Storing data locally
      2. Know the Limitations of Your Ajax Library
    4. Summary
  11. 8. Programming Practices
    1. Avoid Double Evaluation
    2. Use Object/Array Literals
    3. Don’t Repeat Work
      1. Lazy Loading
      2. Conditional Advance Loading
    4. Use the Fast Parts
      1. Bitwise Operators
      2. Native Methods
    5. Summary
  12. 9. Building and Deploying High-Performance JavaScript Applications
    1. Apache Ant
    2. Combining JavaScript Files
    3. Preprocessing JavaScript Files
    4. JavaScript Minification
    5. Buildtime Versus Runtime Build Processes
    6. JavaScript Compression
    7. Caching JavaScript Files
    8. Working Around Caching Issues
    9. Using a Content Delivery Network
    10. Deploying JavaScript Resources
    11. Agile JavaScript Build Process
    12. Summary
  13. 10. Tools
    1. JavaScript Profiling
    2. YUI Profiler
    3. Anonymous Functions
    4. Firebug
      1. Console Panel Profiler
      2. Console API
      3. Net Panel
    5. Internet Explorer Developer Tools
    6. Safari Web Inspector
      1. Profiles Panel
      2. Resources Panel
    7. Chrome Developer Tools
    8. Script Blocking
    9. Page Speed
    10. Fiddler
    11. YSlow
    12. dynaTrace Ajax Edition
    13. Summary
  14. Index
  15. About the Author
  16. Colophon
  17. Copyright

Product information

  • Title: High Performance JavaScript
  • Author(s): Nicholas C. Zakas
  • Release date: March 2010
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596802790