Immutable.js is a library that provides Persistent Immutable data structures for JavaScript. Immutable data cannot be changed once created, which simplifies application development, eliminates the need for defensive copying, and enables advanced memoization and change detection techniques with straightforward logic. Persistent data offers a mutative API which does not update the data in place, instead always returning new updated data. Immutable.js data structures are maximally efficient on modern JavaScript VMs by using structural sharing via hash maps tries and vector tries.
To use Immutable, you need to first install the package using npm, Yarn, pnpm, or Bun. You can then import it into your modules. Here's an example of how to use a Map data structure from Immutable.js:
const { Map } = require('immutable');
const map1 = Map({ a: 1, b: 2, c: 3 });
const map2 = map1.set('b', 50);
console.log(map1.get('b') + ' vs. ' + map2.get('b')); // Outputs: 2 vs. 50
You can also use Immutable.js in a browser environment. It is recommended to use a module bundler like webpack, rollup, or browserify. Alternatively, Immutable.js can be directly included as a script tag or through an AMD-style loader like RequireJS.
For those using TypeScript or Flow, Immutable.js includes type definitions, which allows it to be used seamlessly within TypeScript or Flow programs, with the benefits of generics, type safety, and auto-completion in your IDE.
The documentation for Immutable.js is automatically generated from the README.md file and the immutable.d.ts
file in its GitHub repository. You can access the up-to-date, official documentation at https://immutable-js.com. Additionally, you may find helpful articles on additional specific topics, such as nested structures and batching mutations, in the wiki of the project's GitHub repository. If you can't find something, you're encouraged to open an issue on the GitHub repository.