npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

gattai-merge

v3.4.3

Published

High-performance deep merge utility with structural sharing

Readme

Gattai Merge

High-performance deep merge utility with structural sharing. Supports circular ref and complex built-in types.

Install

npm i gattai-merge
// npm
import gattaiMerge from 'gattai-merge';

// CDNs
import gattaiMerge from 'https://esm.sh/gattai-merge'
// or
import gattaiMerge from 'https://cdn.jsdelivr.net/npm/gattai-merge/+esm';
// or
import gattaiMerge from 'https://unpkg.com/gattai-merge/dist/index.js';

📦 APIs

gattaiMerge(target, ...sources, options)
// => DeepMergedObject<T, S>
// 
// target: T
// ...sources: ...S
// options (optional): GattaiMergeOptions

🪄 Options

interface GattaiMergeOptions {
  arrays?: 'replace' | 'concat' | 'merge' | ArrayMergeFunction; // (default: 'replace')
  nullish?: 'loose' | 'strict' | 'throw';                       // (default: 'loose')
  preserveDescriptors?: boolean;                                // (default: false)
  strictDescriptors?: boolean;                                  // (default: false)
}

arrays

  • 'replace': replace target array (shallow copy)
  • 'concat': concatenate arrays
  • 'merge': deep merge by index
  • ArrayMergeFunction: custom array merge function (advanced usage)

⚙️ ArrayMergeFunction

(target, source, {
  merge: (target, source) => {},
  clone: (node) => {},
}) => {};

nullish

  • 'loose': keep target value if source is nullish
  • 'strict': overwrite target value if source is nullish
  • 'throw': throw TypeError if source is nullish

preserveDescriptors

If true, preserves property descriptors (getters/setters, etc.).

strictDescriptors

If true, throws when descriptor cannot be merged (e.g. non-configurable or non-writable).

📖 Details

Examples

Array

gattaiMerge([1, 2], [3, 4]);
// => [3, 4]

gattaiMerge([1, 2], [3, 4], { arrays: 'concat' });
// => [1, 2, 3, 4]

gattaiMerge([{ a: 1 }], [{ b: 2 }], { arrays: 'merge' });
// => [{ a: 1, b: 2 }]

Custom array merge function

gattaiMerge(
  [{ id: 1, value: 'A' }],
  [{ id: 1, value: 'B' }, { id: 2, value: 'C' }],
  {
    // merge items by id
    arrays: (target, source, { merge, clone }) => {
      const map = new Map();

      for (const item of target) {
        map.set(item.id, item);
      }

      for (const item of source) {
        if (map.has(item.id)) {
          map.set(item.id, merge(map.get(item.id), item));
        } else {
          map.set(item.id, clone(item));
        }
      }

      return Array.from(map.values());
    },
  }
);
// => [{ id: 1, value: 'B' }, { id: 2, value: 'C' }]

Map / Set

gattaiMerge(
  new Map([['a', 1]]),
  new Map([['b', 2]])
);
// => Map { 'a' => 1, 'b' => 2 }

Caution

Gattai Merge is optimized for performance using structural sharing (copy-on-write). Objects are only cloned when a change is actually required.

What this implies

If no changes occur during merging, the original target object is returned as-is:

const a = { x: 1 };
const b = { x: 1 };

const result = gattaiMerge(a, b);

result === a; // true

Important

Because the same ref may be returned, mutating the result can also mutate the original input:

result.x = 2;

console.log(a.x); // 2 (mutated!)

When does this happen?

  • When merging produces no effective changes
  • When merging Map, Set, or nested structures with identical values
  • When structural sharing is preserved for performance

How to avoid this

1. Force a new object
const result = gattaiMerge({}, a, b);
2. Defensive cloning
const result = gattaiMerge(a, b);
const safe = result === a ? { ...result } : result;

Design note

This behavior is intentional and aligns with libraries like Immer, prioritizing performance by avoiding unnecessary cloning.

If you require strict immutability guarantees, consider wrapping or extending the API to always return a new object.

🚀 Benchmark

# Install
npm i tsx tinybench gattai-merge lodash.merge deepmerge deepmerge-ts immer

# Run
npx tsx run.ts

# Cleanup
npm un tsx tinybench gattai-merge lodash.merge deepmerge deepmerge-ts immer