liveBook · Manning

Chapter 10: Dealing with collections

What you will learn in chapter 10 of Secrets of the JavaScript Ninja, Third Edition

The chapter introduces JavaScript collections by distinguishing arrays, maps, and sets, then previews a discussion of immutability.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces JavaScript collections by distinguishing arrays, maps, and sets, then previews a discussion of immutability.

Key terms: arrays, maps, sets, key-value pairs

10.1 Arrays

Introduces JavaScript arrays as dynamic objects, notes their convenience and performance tradeoff, and distinguishes them from typed arrays.

Key terms: array, object, built-in methods, typed arrays, Uint8Array, Int32Array, raw data

10.1.1 Creating arrays

This section explains how to create arrays and how indexing and the length property behave when items are missing or array bounds change.

Key terms: array literal, Array constructor, fill method, index notation, undefined, hole, forEach, length property, empty items

10.1.2 Adding and removing items at either end of an array

Explains how to add and remove items at either end of an array, how arrays can serve as stacks or queues, and why operations at the beginning can be costly.

Key terms: push, pop, shift, unshift, dynamic arrays, stack, queue, last in, first out, first in, first out, moved in memory

10.1.3 Adding and removing items at any array location

This section explains how deleting an array item differs from using splice, and distinguishes splice from slice and array-copying syntax.

Key terms: delete operator, empty slot, splice, index, slice, spread syntax

10.1.4 Common operations on arrays

This section explains how to traverse arrays, transform and test their items, search and sort them, and accumulate a result.

Key terms: for…of, for…in, forEach, break, await, map, flat, flatMap, callback, every

10.2 Maps

This section explains why Maps can be preferable to objects for storing key-value pairs: they contain only explicitly stored entries and accept keys of any type.

Key terms: prototypes, Object.create(null), Maps, key-value pairs, object keys, symbols, keys

10.2.1 Creating our first map

This section shows how to create a typed map, use object keys for separate metadata, and determine when two keys identify the same entry.

Key terms: Map, Key, Value, type parameter, object keys, external metadata, clear(), garbage collection, map key, key1 === key2

10.2.2 Iterating over maps

This section explains how to iterate over maps in insertion order, choose between entries and key-only or value-only iterators, and convert between maps and objects.

Key terms: for...of, tuple, array destructuring, entry, keys, values, iterator objects, entries, Object.entries, Object.fromEntries

10.2.3 Weakly holding references with WeakMap

Explains how WeakMap avoids retaining discarded object keys and their metadata, and what limitations come with using it instead of Map.

Key terms: Map, WeakMap, weak references, garbage-collection, memory leaks, objects as keys, primitives, size property, get(key), iterate

10.3 Sets

Introduces sets as collections of distinct items and contrasts them with using a map to track those items.

Key terms: sets, distinct items, map, keys, values

10.3.1 Creating our first set

This section introduces Set construction, its item-based API, and why sets are useful for repeated membership checks.

Key terms: Set, constructor, add(item), Map API, has, size, iterator, entries, array.includes(item), set.has(item)

10.3.2 Combining and comparing sets

This section introduces ES2024 methods for combining and comparing sets and notes when they are practical to use.

Key terms: union, intersection, difference, isSubsetOf, ES2024, runtime, Node.js 22

10.3.3 Weakly holding references with WeakSet

This section explains how WeakSet tracks objects without preventing their garbage collection, along with its main limitations.

Key terms: WeakSet, garbage-collected, memory leaks, objects, primitives, size property, iterate

10.4 Mutable and immutable collections

Introduces mutable and immutable collections, why unexpected mutation matters, and the costs of making changes without mutation.

Key terms: mutable collections, mutated, anti-pattern, immutable collections, in-place

10.4.1 The perils of mutability

This section uses a React to-do list to show why collection updates should preserve the original state, then compares ways to avoid mutation.

Key terms: React state, todos, push, setTodos, spreading, immutable operation, Immutable.js, List, mutable collection, ESLint

10.4.2 Read-only collections in TypeScript

This section shows how TypeScript read-only collection types help prevent unintended mutation at compile time, while explaining their limits and the precise types available through `as const`.

Key terms: as const, readonly, NinjaName, string union, read-only type, compile-time error, runtime error, reassigned, ReadonlyMap, ReadonlySet

10.4.3 Immutable transformations with Immer

This section explains why nested immutable updates are difficult and how Immer creates updated copies through a mutable-looking draft.

Key terms: spread operator, destructuring, shallow, deep copy, nested objects, Immer, produce, draft, Proxy, NinjaState

10.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 9: Objects, prototypes, and classes Chapter 11: Buffers and streams →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.