Discuss your project

Turbocharging React with Million.js: An Exploration

/* by Tirth Bodawala - June 14, 2023 */

The Birth of a Speedster

Million.js is a product of rethinking how the Virtual DOM can be managed. It brings to the table a unique and innovative concept of “block” virtual DOM that contrasts with traditional methods. Instead of diffing the DOM like React, Million.js diffs data. The result? A significant boost in performance, clocking in at up to 70% faster than standard React components.

However, it’s worth noting that these benchmarks might not directly translate into real-world performance. The improvement could be even more substantial, particularly in scenarios involving heavy data or UI-intensive applications.

Integrating Million.js into React

One of the most appealing aspects of Million.js is the ease of integration with existing React components. There’s no requirement to learn a new framework or overhaul your codebase. The primary tool you’ll need is a function provided by Million.js named block(). Wrapping your React components in this function enables them to benefit from the speed optimizations offered by Million.js.

Here’s an example of how you can wrap a React “counter” component using Million.js:

import { useState } from 'react';
import { block } from 'million/react';

// Just wrap Counter in a block() function!
const Counter = block(function Counter({ initialCount }) {
  const [count, setCount] = useState(initialCount);
  const handleClick = () => {
    setCount(count + 1);
  };

  return {count};
});

export default Counter;

This easy integration makes Million.js a tempting proposition for developers looking to improve the performance of their React applications without significant rewrites or learning new technologies.

The Architecture of Million.js

Million.js is not only powerful but also extremely lightweight and modular. The entire library comes under 4kb, which contributes to its speed. It’s organized as a mono-repo with modules catering to different functionalities, ranging from React compatibility to a custom compiler that optimizes React components on the server. This modular design offers a degree of flexibility, allowing developers to use only what they need.

A Real-World Experiment

In an experiment that attempted to render a staggering one million items, Million.js paired with React outperformed plain React, which failed to render even 100k items. While this is an extreme case, it demonstrates the potential of Million.js to handle large amounts of data effectively.

Getting Started with Million.js

Getting started with Million.js is straightforward. A quick-start guide is available on GitHub, providing step-by-step instructions on how to integrate Million.js into your existing React projects. Whether you’re running a project in development mode or bundling it for production, the guide covers everything you need to know.

Wrapping Up

Million.js represents an exciting development in the realm of JavaScript libraries. It promises to provide a significant performance boost to React applications with a minimal learning curve, which is a dream come true for many developers. However, it’s essential to remember that benchmarks don’t always equate to real-world performance. As always, you should test Million.js in the context of your projects to evaluate its impact accurately.

References

Official Million.js Website: The official website provides a brief overview of Million.js, its features, and how it works.

Million.js GitHub Repository: This is the official GitHub repository for Million.js. It contains the library’s source code, a readme with basic information and usage examples, and other resources.

Vite Starter for Million.js: This is a starter template for Million.js projects using Vite. It provides a basic project structure and setup instructions.

ReactJS Example: This is an article that attempts to visualize the performance of Million.js compared to React.js in rendering a large number of items.

I would recommend starting with the official website and GitHub repository to get a basic understanding of the library, and then move on to the Vite starter for practical usage and the ReactJS Example article for a performance comparison.