:fire: An extremely fast, React-like JavaScript library for building modern user interfaces
:fire: An extremely fast, React-like JavaScript library for building modern user interfaces
Inferno is an insanely fast, React-like library for building high-performance user interfaces on both the client and server.
The main objective of the InfernoJS project is to provide the fastest possible runtime performance for web applications. Inferno excels at rendering real time data views or large DOM trees.
The performance is achieved through multiple optimizations, for example:
createVNode calls, instead of createElement calls.
Optimizing runtime performance of the application. linkEvent feature removes the need to use arrow functions or binding event callbacksinferno-servercreatePortal - API or using object literal syntax . For camelCase syntax support see inferno-compat.Inferno v9 requires following features to be present in the executing runtime:
PromiseString.prototype.includes()String.prototype.startsWith()Array.prototype.includes()Object.spread()for ... ofSince version 4 we have started running our test suite without any polyfills. Inferno is now part of Saucelabs open source program and we use their service for executing the tests.
InfernoJS is actively tested with browsers listed below, however it may run well on older browsers as well. This is due to limited support of browser versions in recent testing frameworks. https://github.com/jasmine/jasmine/blob/main/release_notes/5.0.0.md
Live examples at https://infernojs.github.io/inferno
Let's start with some code. As you can see, Inferno intentionally keeps the same design ideas as React regarding components: one-way data flow and separation of concerns.
In these examples, JSX is used via the Inferno JSX Babel Plugin to provide a simple way to express Inferno virtual DOM. You do not need to use JSX, it's completely optional, you can use hyperscript or createElement (like React does). Keep in mind that compile time optimizations are available only for JSX.
import { render } from 'inferno';
const message = "Hello world";
render(
<MyComponent message={ message } />,
document.getElementById("app")
);Furthermore, Inferno also uses ES6 components like React:
import { render, Component } from 'inferno';
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0
};
}
render() {
return (
);
}
}
render(
<MyComponent />,
document.getElementById("app")
);Because performance is an important aspect of this library, we want to show you how to optimize your application even further.
In the example below we optimize diffing process by using JSX $HasVNodeChildren and $HasTextChildren to predefine children shape compile time.
In the MyComponent render method there is a div that contains JSX expression node as its content. Due to dynamic nature of Javascript
that variable node could be anything and Inferno needs to go through the normalization process to make sure there are no nested arrays or other invalid data.
Inferno offers a feature called ChildFlags for application developers to pre-define the shape of vNode's child node. In this example case
it is using $HasVNodeChildren to tell the JSX compiler, that this vNode contains only single element or component vNode.
Now inferno will not go into the normalization process runtime, but trusts the developer decision about the shape of the object and correctness of data.
If this contract is not kept and node variable contains invalid value for the pre-defined shape (fe. null), then application would crash runtime.
There is also span-element in the same render method, which content is set dynamically through _getText() method. There $HasTextChildren child-flag
fits nicely, because the content of that given "span" is never anything else than text.
All the available child flags are documented here.
import { createTextVNode, render, Component } from 'inferno';
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0
};
}
_getText() {
return 'Hello!';
}
render() {
const node = this.state.counter > 0 ?
: <span $HasTextChildren>{this._getText()}</span>;
return (
</div>
);
}
}
render(
<MyComponent />,
document.getElementById("app")
);To tear down inferno application you need to render null on root element.
Rendering null will trigger unmount lifecycle hooks for whole vDOM tree and remove global event listeners.
It is important to unmount unused vNode trees to free browser memory.
import { createTextVNode, render, Component } from 'inferno';
const rootElement = document.getElementById("app");
// Start the application
render(
<ExampleComponent/>,
rootElement
);
// Tear down
render(
null,
rootElement
);
If you have built something using Inferno you can add them here:
The easiest way to get started with Inferno is by using Create Inferno App.
Alternatively, you can try any of the following:
Core package:
npm install --save infernoAddons:
# server-side rendering
npm install --save inferno-server
# routing
npm install --save inferno-routerPre-bundled files for browser consumption can be found on our cdnjs:
Or on jsDelivr:
https://cdn.jsdelivr.net/npm/inferno@latest/dist/inferno.min.jsOr on unpkg.com:
https://unpkg.com/inferno@latest/dist/inferno.min.jsnpm install --save-dev babel-plugin-infernonpm install --save inferno-hyperscriptnpm install --save inferno-create-elementnpm install --save-dev inferno-compatNote: Make sure you read more about inferno-compat before using it.
Inferno now has bindings available for some of the major state management libraries out there:
inferno-reduxinferno-mobx@cerebral/infernoInferno has its own JSX Babel plugin.
onClick).createRef or callback ref APIPrompt breaks back button in iOS Safari
Add compile time optimizaton for syntehtic event handling
<input onClick="string()"/> throws TypeError
useState hook
Implement a Vite plugin
Duplicated isValidElement from inferno-compat in inferno-router/src/utils.ts
SSR tests are run in JSDOM env and rely on document
re-design lifecycle parameter
Add implementation type aliases to make types safe application development easier
Add support for math formula elements