Book Image

Mastering Immutable.js

By : Adam Boduch
Book Image

Mastering Immutable.js

By: Adam Boduch

Overview of this book

Immutable.js is a JavaScript library that will improve the robustness and dependability of your larger JavaScript projects. All aspects of the Immutable.js framework are covered in this book, and common JavaScript situations are examined in a hands-on way so that you gain practical experience using Immutable.js that you can apply across your own JavaScript projects. The key to building robust JavaScript applications using immutability is to control how data flows through your application, and how the side-effects of these flows are managed. Many problems that are difficult to pinpoint in large codebases stem from data that’s been mutated where it shouldn’t have been. With immutable data, you rule out an entire class of bugs. Mastering Immutable.js takes a practical, hands-on approach throughout, and shows you the ins and outs of the Immutable.js framework so that you can confidently build successful and dependable JavaScript projects.
Table of Contents (23 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Limiting results and reducing work


You've seen how to chain sequence operations together. By doing this, you can pass one value at a time to your chain and into your side-effect. This lets you write legible code that doesn't create intermediary collections between operations. However, you can still run into performance problems with large collections because you'll still have to iterate over every value.

Using take() to limit results

Use the take() method to tell sequences that you only want a specific number of results. For example, if a page in your application wants to display 20 values, you could add take(20) to your sequence chain. Where should you add this call? The order of where take() is placed in the chain of method calls can have dramatic effects on the result.

Let's start by creating an infinite sequence, as shown in the following code block:

import { Range } from 'immutable';

const myRange = Range();

Here, Range is a special type of sequence that's handy for creating large sequences...