Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active June 11, 2024 18:10
Show Gist options
  • Save stephenlb/ddb0d5e182b5c5ba2d0f549913778877 to your computer and use it in GitHub Desktop.
Save stephenlb/ddb0d5e182b5c5ba2d0f549913778877 to your computer and use it in GitHub Desktop.
ReactJS vs Preact JS - Differences and Performance

ReactJS vs Preact: New Project

Preact has smaller library files compared to React. Here is a detailed comparison:

Library Size

  • React: The minified and gzipped production build of React typically weighs around 60 KB.
  • Preact: The minified and gzipped version of Preact is significantly smaller, approximately 6 KB.

Other Considerations

  1. API Compatibility: Preact aims to be as compatible as possible with React's API, but there are some differences and limitations. For example, not all of React's advanced features or third-party libraries may work seamlessly with Preact.
  2. Performance: Preact's smaller size can lead to faster initial load times, which is especially beneficial for performance-critical applications. However, both libraries offer excellent runtime performance.
  3. Ecosystem: React has a larger ecosystem and more community support, which means more third-party libraries, tools, and resources are available.
  4. Development Experience: If your team is already familiar with React, transitioning to Preact should be relatively straightforward due to the similar API. However, some adjustments may be needed for full compatibility.
  5. Features: Advanced React features like Concurrent Mode, suspense, and extensive dev tools support are areas where React has an edge. While Preact continuously improves, it might not always be on par with the latest React features.

Example Use Cases

  • Preact: Ideal for smaller projects, static sites, or projects where initial load performance is a critical factor.
  • React: Better suited for larger applications that might benefit from the extensive ecosystem and advanced features.

Conclusion

While Preact offers a compelling alternative due to its smaller size and fast load times, the choice between React and Preact should be based on the specific needs of your project and the trade-offs you are willing to make in terms of ecosystem, features, and compatibility.

If you have any more questions or need further details on any specific aspect, feel free to ask!

ReactJS vs Preact: Migrating Existing Project

Using preact-compat (now integrated as preact/compat) allows you to run React components in a Preact environment, effectively combining the benefits of Preact's smaller size with React's vast ecosystem. However, the extent of file size reduction and the advantages you achieve depend on various factors. Here are some key points to consider:

File Size Reduction

  1. Base Library Sizes:

    • React: Around 60 KB minified and gzipped.
    • Preact + preact/compat: Around 10 KB minified and gzipped.
  2. Components and Libraries: If your application heavily uses third-party React components and libraries that are fully compatible with preact/compat, you can still achieve a considerable reduction in the overall bundle size since the core library (Preact) is much smaller.

Advantages

  1. Improved Load Times: The reduced bundle size can lead to faster initial load times, which is especially beneficial for performance-critical applications and projects targeting slower networks or lower-end devices.
  2. Development Flexibility: By using preact/compat, you can leverage existing React components, tools, and libraries without significant refactoring, while still benefiting from the performance improvements of Preact.
  3. Ease of Migration: For projects looking to migrate to a smaller footprint without a complete rewrite, switching to Preact with preact/compat provides a smoother transition path.

Potential Disadvantages

  1. Compatibility Issues: While preact/compat provides a high degree of compatibility, there may be edge cases where certain React components or libraries do not work perfectly, especially if they use low-level React internals not fully emulated by Preact.
  2. Advanced Features: More advanced features, such as Concurrent Mode and Suspense, may not be as well-supported or may require additional configurations.
  3. Performance Trade-offs: While initial load performance benefits from the smaller size, runtime performance characteristics might differ, especially for highly complex applications.

Practical Example

Assuming you have an existing React application, here’s a basic overview of steps to utilize Preact with preact/compat:

  1. Install Preact and Compatibility Layer:

    npm install preact preact/compat
  2. Alias React to Preact in Your Build: Configure your bundler (Webpack, Rollup, etc.) to alias React to Preact:

    // webpack.config.js
    module.exports = {
      // ... your existing config
      resolve: {
        alias: {
          'react': 'preact/compat',
          'react-dom': 'preact/compat'
        }
      }
    };
  3. Run Your App and Optimize: Build and run your application as usual. The final bundle size should reflect the smaller footprint of Preact. Monitor and test thoroughly to ensure compatibility and performance.

Conclusion

Using Preact with preact/compat is a viable strategy to reduce file size while still leveraging the React ecosystem. Always test performance, compatibility, and functionality thoroughly to ensure it meets your application's requirements.

If you have further questions or need more detailed guidance, feel free to ask!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment