Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Mutative 2.0.0 was 3.6x faster than Immer 11.1.18 with matched settings and 6.6x
<details>
<summary><strong>Selected results</strong></summary>

The [benchmark suite](./perf-testing/README.md) times 99 workloads: Immer's own performance tests, array methods, reads, Map and Set values, object records, class instances, a deep path, patch application, returned values, and searches. Its last full run measured 97 of them, and a focused run the two that return fresh data. It compares Mutative with Immer 11.1.18 and with reducers written by hand, after checking every result against those reducers. With matched settings, both libraries freeze or both do not, and both generate patches or both do not. With each library's defaults, Mutative without auto-freeze and Immer with it, Mutative was faster in 154 of 157 cases. The other 3 return a state that Immer froze earlier: Immer skips frozen values when it searches a returned state for drafts, while Mutative, which does not freeze by default, searches it unless the recipe wraps it in [`rawReturn()`](https://mutative.js.org/docs/api-reference/rawreturn#performance-compared-with-immer), which took 0.26 µs.
The [benchmark suite](./perf-testing/README.md) times 101 workloads: Immer's own performance tests, array methods, sparse arrays, reads, Map and Set values, object records, class instances, a deep path, patch application, returned values, and searches. Its last full run measured 97 of them, and a focused run the two that return fresh data; the two sparse-array workloads came later. It compares Mutative with Immer 11.1.18 and with reducers written by hand, after checking every result against those reducers. With matched settings, both libraries freeze or both do not, and both generate patches or both do not. With each library's defaults, Mutative without auto-freeze and Immer with it, Mutative was faster in 154 of 157 cases. The other 3 return a state that Immer froze earlier: Immer skips frozen values when it searches a returned state for drafts, while Mutative, which does not freeze by default, searches it unless the recipe wraps it in [`rawReturn()`](https://mutative.js.org/docs/api-reference/rawreturn#performance-compared-with-immer), which took 0.26 µs.

Times are microseconds per update, medians of three runs on an Apple M1 Max with Node.js 24.16.0, each library in a process of its own; lower is better. Mutative, the first Immer column, and the hand-written reducers run without auto-freeze; the second Immer column shows Immer's default.

Expand Down Expand Up @@ -165,7 +165,7 @@ CDN

The package's other ESM files read `process.env.NODE_ENV`, which bundlers and Node.js provide; a browser without a bundler needs the production file above.

Mutative has no dependencies. A production bundle of `create` alone is about 7.9 kB after Brotli compression, and one of `create`, `apply`, `current` and `original` about 8.5 kB, including patches, `Map` and `Set` support and native array methods, which need no plugins.
Mutative has no dependencies. A production bundle of `create` alone is about 8.3 kB after Brotli compression, and one of `create`, `apply`, `current` and `original` about 8.9 kB, including patches, `Map` and `Set` support and native array methods, which need no plugins.

## Usage

Expand Down Expand Up @@ -827,6 +827,7 @@ Mutative v2 keeps the v1 API. The changes below, made since v1.3.0, can affect e
- A Set draft that the recipe moves to another place, such as another key, an array, a Map, another Set or a new object, keeps its added, deleted and changed items there when its old place gets another value or is removed; v1 kept its original items.
- A draft of a Set subclass changes its copy, an instance of the subclass, as Map drafts do: the subclass's own `add`, `has`, `delete` and `clear` take effect during the recipe, while the draft iterates the items in the order in which the Set holds them. v1 kept the items of a changed Set in a separate map, which bypassed those methods until it rebuilt the Set after the recipe. As a Map subclass's `set` already had to in v1, a Set subclass's `add` must store the value that it receives: an `add` that stores a copy of a draft, or of an object that holds drafts, leaves those drafts in the next state, where reading them throws.
- Assigning `undefined` to a key that `delete`, `shift`, `unshift` or a shrinking `splice` removed from a draft adds the key back; v1 left a hole or kept the shorter length.
- Deleting an array index that the current draft does not own changes nothing, as in JavaScript. In v1, deleting an index already absent from the original array could create an `undefined` element and, if it was past the end, lengthen the array. This also fixes the v2.0.0 regression where deleting an index removed earlier in the same recipe grew the array again; v1 kept the shortened array ([#212](https://github.com/unadlib/mutative/issues/212)).
- Assigning to a key that a draft holds as an own data property sets that property, as in JavaScript, also when a prototype defines a setter of that name. v1 called the setter through the draft instead, and assigning an own `__proto__` key, as `JSON.parse()` creates one, threw. A class instance that `mark` makes `immutable` is copied by assigning its own properties, so such a setter now runs on that copy instead of through the draft; see [Copying marked instances](https://mutative.js.org/docs/advanced-guides/mark#copying-marked-instances).
- Under a `mark` that returns `mutable`, a value that the recipe assigned or moved is read back as assigned, through the draft and in `current()`; v1 returned the original value.
- `current()` of a draft whose state holds a plain Set with drafts returns a snapshot; v1 threw.
Expand All @@ -842,7 +843,7 @@ Mutative v2 keeps the v1 API. The changes below, made since v1.3.0, can affect e
- A patch for a Map key that is an array holds the key as one path segment; v1 spread the array into the path, so applying the patch wrote to other keys.
- `apply()` uses a Map key of a patch path as it is. v1 converted it to a string first, which threw for an object without a prototype.
- Array patches record a change between `0` and `-0`, which the state already kept; in v1, applying the patches lost the sign. An element that stays `NaN` no longer yields a replace patch.
- When a recipe shortens an array and lengthens it again, as `draft.length = 1; draft.length = 3` does, the patches replace the indices that the longer length exposes again. In v1, applying the patches kept the removed elements at those indices, and applying the inverse patches left holes there.
- Patches keep the holes of sparse arrays apart from `undefined` elements, in both directions. When a recipe shortens an array and lengthens it again, as `draft.length = 1; draft.length = 3` does, the patches shorten and lengthen the array as well, which leaves holes at the indices that the longer length exposes again, and the inverse patches put the removed elements back. In v1, applying the patches kept the removed elements at those indices, and applying the inverse patches left holes there. v1 also put `undefined` into the holes that an array got past its end, and where a recipe filled a hole, applying the inverse patches put `undefined` there.
- In strict mode, a nested `unsafe()` call no longer ends the access of the outer call; in v1, reading mutable data after it in the outer callback threw.
- A producer that fails after its recipe returned, for example because the recipe changed the draft and returned another value, revokes its drafts and releases its array method cache, as a recipe that throws does; v1 left them usable. This also covers errors while inspecting a returned Proxy or calling a returned Promise's `then` method, and preserves the original error.
- A draft of an array whose `Symbol.isConcatSpreadable` is false copies its elements; v1 put the whole array into the copy as its only element.
Expand Down
2 changes: 2 additions & 0 deletions docs/type-aliases/PatchesOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ does not.
Whether an array that gets shorter gets a `replace` patch of its
`length`, as by default, or a `remove` patch for each index that it
loses, as JSON Patch requires.
Recreating holes in a sparse array still uses length patches;
JSON cannot represent holes.

### pathAsArray?

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mutative",
"version": "2.0.0",
"version": "2.0.1",
"description": "A JavaScript library for efficient immutable updates",
"main": "dist/index.js",
"module": "dist/mutative.esm.js",
Expand Down Expand Up @@ -62,19 +62,19 @@
"name": "production CJS, all exports",
"path": "dist/mutative.cjs.production.min.js",
"import": "*",
"limit": "8.7 KB"
"limit": "9.1 KB"
},
{
"name": "ESM, create",
"path": "dist/mutative.esm.mjs",
"import": "{ create }",
"limit": "7.7 KB"
"limit": "8.1 KB"
},
{
"name": "ESM, all exports",
"path": "dist/mutative.esm.mjs",
"import": "*",
"limit": "8.6 KB"
"limit": "9.0 KB"
}
],
"repository": {
Expand Down
Loading
Loading