{"library":"mutative","title":"Mutative","description":"Mutative is a JavaScript library designed for performing efficient immutable updates on data structures, drawing inspiration from libraries like Immer but with a strong focus on performance. It allows developers to write 'mutative' logic inside a producer function, and Mutative handles the underlying copy-on-write mechanism to return a new, immutably updated state. The library is currently at version 1.3.0 and exhibits an active release cadence, with frequent patch and minor updates. Key differentiators include its reported performance benefits, claiming to be 2-6x faster than naive handcrafted reducers using spread operators and over 10x faster than Immer, primarily through shallow copy optimization, lazy drafts, and an optimized finalization process. It aims to address issues like Immer's mandatory auto-freeze and improve type inference and handling of various immutability edge cases.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mutative"],"cli":null},"imports":["import { create } from 'mutative'","import { apply } from 'mutative'","import { original } from 'mutative'","import type { IPatch } from 'mutative'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { create, original } from 'mutative';\n\ninterface TodoItem {\n  id: number;\n  text: string;\n  completed: boolean;\n}\n\ninterface AppState {\n  todos: TodoItem[];\n  filter: 'all' | 'active' | 'completed';\n  user: { name: string; id: string };\n}\n\nconst initialState: AppState = {\n  todos: [\n    { id: 1, text: 'Learn Mutative', completed: false },\n    { id: 2, text: 'Build Something', completed: true },\n  ],\n  filter: 'all',\n  user: { name: 'John Doe', id: 'user-123' },\n};\n\n// Update an item and add a new one\nconst newState = create(initialState, (draft) => {\n  const todoToUpdate = draft.todos.find(todo => todo.id === 1);\n  if (todoToUpdate) {\n    todoToUpdate.completed = true;\n    todoToUpdate.text = 'Learn Mutative (done!)';\n  }\n  draft.todos.push({ id: 3, text: 'Deploy App', completed: false });\n  draft.user.name = 'Jane Doe';\n});\n\nconsole.log('Initial State:', original(initialState));\nconsole.log('New State:', newState);\nconsole.log('Are states different?', initialState !== newState); // true\nconsole.log('Is todos array different?', initialState.todos !== newState.todos); // true\nconsole.log('Are other parts (e.g., filter) unchanged by reference?', initialState.filter === newState.filter); // true\nconsole.log('Original todo 1 text (from initial state):', initialState.todos[0]?.text); // Learn Mutative\nconsole.log('New todo 1 text (from new state):', newState.todos[0]?.text); // Learn Mutative (done!)\n","lang":"typescript","description":"Demonstrates how to use `create` to immutably update nested objects and arrays, showcasing efficient partial updates and reference preservation for unchanged parts.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}