JavaScript Design Patterns

Book description

Step into the world of design patterns through modern JavaScript paradigms to supercharge your applications on the web and beyond

Key Features

  • Explore various JavaScript design patterns, delving deep into their intricacies, benefits, and best practices
  • Understand the decision-making process guiding the selection of specific design patterns
  • Build a solid foundation to learn advanced topics in JavaScript and web performance
  • Purchase of the print or Kindle book includes a free PDF eBook

Book Description

Unlock the potential of JavaScript design patterns, the foundation for development teams seeking structured and reusable solutions to common software development challenges in this guide to improving code maintainability, scalability, and performance. Discover how these patterns equip businesses with cleaner and more maintainable code, promote team collaboration, reduce errors, and save time and costs.

This book provides a comprehensive view of design patterns in modern (ES6+) JavaScript with real-world examples of their deployment in professional settings. You’ll start by learning how to use creational, structural, and behavioral design patterns in idiomatic JavaScript, and then shift focus to the architecture and UI patterns. Here, you’ll learn how to apply patterns for libraries such as React and extend them further to general web frontend and micro frontend approaches. The last section of the book introduces and illustrates sets of performance and security patterns, including messaging and events, asset and JavaScript loading strategies, and asynchronous programming performance patterns.

Throughout the book, examples featuring React and Next.js, in addition to JavaScript and Web API examples, will help you choose and implement proven design patterns across diverse web ecosystems, transforming the way you approach development.

What you will learn

  • Find out how patterns are classified into creational, structural, and behavioral
  • Implement the right set of patterns for different business scenarios
  • Explore diverse frontend architectures and different rendering approaches
  • Identify and address common asynchronous programming performance pitfalls
  • Leverage event-driven programming in the browser to deliver fast and secure applications
  • Boost application performance using asset loading strategies and offloading JavaScript execution

Who this book is for

This book is for developers and software architects who want to leverage JavaScript and the web platform for enhanced productivity, superior software quality, and optimized application performance. Prior experience with JavaScript and web development is assumed. Some of the more advanced topics in the book will be of interest to developers with intermediate experience in building for the web with JavaScript.

Table of contents

  1. JavaScript Design Patterns
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
    8. Download a free PDF copy of this book
  6. Part 1:Design Patterns
  7. Chapter 1: Working with Creational Design Patterns
    1. What are creational design patterns?
    2. Implementing the prototype pattern in JavaScript
      1. Implementation
      2. A use case
    3. The singleton pattern with eager and lazy initialization in JavaScript
      1. Implementation
      2. Use cases
      3. Improvements with the “class singleton” pattern
      4. A singleton without class fields using ES module behavior
    4. The factory pattern in JavaScript
      1. Implementation
      2. Use cases
      3. Improvements with modern JavaScript
    5. Summary
  8. Chapter 2: Implementing Structural Design Patterns
    1. Technical requirements
    2. What are structural design patterns?
    3. Implementing the Proxy pattern with Proxy and Reflect
      1. A redaction proxy implementation
      2. Use cases
      3. Improving the proxy pattern in JavaScript with the Proxy and Reflect global objects
    4. Decorator in JavaScript
      1. Implementation
      2. Use cases
      3. Improvements/limitations
    5. Flyweight in JavaScript
      1. Implementation
      2. Use cases
      3. Improvements/limitations
    6. Adapter in JavaScript
      1. Use cases
      2. Improvements/limitations
    7. Summary
  9. Chapter 3: Leveraging Behavioral Design Patterns
    1. Technical requirements
    2. What are behavioral design patterns?
    3. The observer pattern in JavaScript
      1. Implementation
      2. Use cases of the observer pattern
      3. Limitations and improvements
    4. State and strategy in JavaScript and a simplified approach
      1. Implementation
      2. Use cases of the state and strategy patterns
      3. Limitations and improvements
    5. Visitor in JavaScript
      1. Implementation
      2. Use cases of the visitor pattern
    6. Summary
  10. Part 2:Architecture and UI Patterns
  11. Chapter 4: Exploring Reactive View Library Patterns
    1. Technical requirements
    2. What are reactive view library patterns?
    3. The render prop pattern
      1. Use cases
      2. Implementation/example
      3. Limitations
    4. The higher-order component pattern
      1. Implementation/example
      2. Use cases
      3. Limitations
    5. The hooks pattern
      1. An implementation/example
      2. Use cases
      3. Limitations
    6. The provider pattern
      1. Use case – the prop drilling problem
      2. An implementation/example
      3. Limitations
    7. Summary
  12. Chapter 5: Rendering Strategies and Page Hydration
    1. Technical requirements
    2. Client and server rendering with React
      1. Client-side rendering in React
      2. Server rendering in React
      3. Trade-offs between client and server rendering
    3. Static rendering with Next.js
    4. Automatic static generation
    5. Static generation with a third-party data source
    6. Static generation with dynamic paths
    7. Page hydration strategies
      1. Common React rehydration issues
      2. React streaming server-side rendering
    8. Summary
  13. Chapter 6: Micro Frontends, Zones, and Islands Architectures
    1. Technical requirements
    2. An overview of micro frontends
      1. Key benefits
      2. “Classic” micro frontend patterns
      3. Other concerns in a micro frontend world
    3. Composing applications with Next.js “zones”
      1. Root app
      2. Adding a /search app
      3. Adding /checkout app
      4. The benefits/supporting team scaling
      5. The drawbacks of Next.js zones
    4. Scaling performance-sensitive pages with the “islands” architecture
      1. Islands setup with is-land
      2. Product island
      3. Cart island
      4. A related products island
      5. Scaling with a team – bundling islands
      6. Drawbacks
    5. Summary
  14. Part 3:Performance and Security Patterns
  15. Chapter 7: Asynchronous Programming Performance Patterns
    1. Technical requirements
    2. Controlling sequential asynchronous operations with async/await and Promises
    3. Parallel asynchronous operation patterns
    4. Asynchronous cancellation and timeouts with AbortController
    5. Throttling, debouncing, and batching asynchronous operations
    6. Summary
  16. Chapter 8: Event-Driven Programming Patterns
    1. Technical requirements
    2. Optimizing event listeners through event delegation
    3. Patterns for secure frame/native WebView bridge messaging
    4. Event listener performance antipatterns
    5. Summary
  17. Chapter 9: Maximizing Performance – Lazy Loading and Code Splitting
    1. Technical requirements
    2. Dynamic imports and code splitting with Vite
    3. Route-based code splitting and bundling
    4. Loading JavaScript on element visibility and interaction
    5. Summary
  18. Chapter 10: Asset Loading Strategies and Executing Code off the Main Thread
    1. Technical requirements
    2. Asset loading optimization – async, defer, preconnect, preload, and prefetch
    3. Using Next.js Script’s strategy option to optimize asset loading
    4. Loading and running scripts in a worker thread
    5. Summary
  19. Index
    1. Why subscribe?
  20. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Download a free PDF copy of this book

Product information

  • Title: JavaScript Design Patterns
  • Author(s): Hugo Di Francesco
  • Release date: March 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781804612279