https://github.com/tomeraberbach/weak-merge
🔗 A module for merging WeakSets and WeakMaps.
https://github.com/tomeraberbach/weak-merge
javascript npm-package sets union weakmap weakset
Last synced: about 1 month ago
JSON representation
🔗 A module for merging WeakSets and WeakMaps.
- Host: GitHub
- URL: https://github.com/tomeraberbach/weak-merge
- Owner: TomerAberbach
- License: apache-2.0
- Created: 2021-02-23T04:43:22.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-15T03:43:57.000Z (6 months ago)
- Last Synced: 2025-03-10T14:54:58.911Z (about 2 months ago)
- Topics: javascript, npm-package, sets, union, weakmap, weakset
- Language: TypeScript
- Homepage: https://npm.im/weak-merge
- Size: 595 KB
- Stars: 21
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- Contributing: contributing.md
- License: license
Awesome Lists containing this project
README
weak-merge
A module for merging WeakSets and WeakMaps.## Install
```sh
$ npm i weak-merge
```## Usage
```js
import { mergeWeakMaps, mergeWeakSets } from 'weak-merge'const [a, b, c, d] = [{}, {}, {}, {}]
const weakSet1 = new WeakSet([a, b])
const weakSet2 = new WeakSet([c])const mergedWeakSet = mergeWeakSets(weakSet1, weakSet2)
console.log([a, b, c].map(key => mergedWeakSet.has(key)))
//=> [ true, true, true ]mergedWeakSet.delete(a)
console.log(mergedWeakSet.has(a))
//=> falseconsole.log(weakSet1.has(a))
//=> truemergedWeakSet.add(d)
console.log(mergedWeakSet.has(d))
//=> trueconsole.log(weakSet1.has(d))
//=> falseconst weakMap1 = new WeakMap([
[a, 1],
[b, 2],
])
const weakMap2 = new WeakMap([[c, 3]])const mergedWeakMap = mergeWeakMaps(weakMap1, weakMap2)
console.log([a, b, c].map(key => mergedWeakMap.get(key)))
//=> [ 1, 2, 3 ]mergedWeakMap.delete(a)
console.log(mergedWeakMap.has(a))
//=> falseconsole.log(weakMap1.has(a))
//=> truemergedWeakMap.set(a, 5)
console.log(mergedWeakMap.get(a))
//=> 5console.log(weakMap1.get(a))
//=> 1
```See the
[TypeScript types](https://github.com/TomerAberbach/weak-merge/blob/main/src/index.d.ts)
for more documentation.## Why?
Merging `WeakSet` or `WeakMap` instances is not trivial because they
[are not enumerable](https://javascript.info/weakmap-weakset).## Performance
`WeakSet` instances returned from `mergeWeakSets` and `WeakMap` instances
returned from `mergeWeakMaps` are not as performant as native `WeakSet` and
`WeakMap` instances (due to the lack of a native way to merge or copy `WeakSet`
and `WeakMap` instances):### `WeakSet` Time Complexity
| Operation | Native `WeakSet` | Merge of `n` native `WeakSet` instances |
| --------- | ---------------- | --------------------------------------- |
| `add` | `O(1)` | `O(1)` |
| `delete` | `O(1)` | `O(1)` |
| `has` | `O(1)` | `O(n)` |### `WeakMap` Time Complexity
| Operation | Native `WeakMap` | Merge of `n` native `WeakMap` instances |
| --------- | ---------------- | --------------------------------------- |
| `delete` | `O(1)` | `O(1)` |
| `get` | `O(1)` | `O(n)` |
| `has` | `O(1)` | `O(n)` |
| `set` | `O(1)` | `O(1)` |## Contributing
Stars are always welcome!
For bugs and feature requests,
[please create an issue](https://github.com/TomerAberbach/weak-merge/issues/new).For pull requests, please read the
[contributing guidelines](https://github.com/TomerAberbach/weak-merge/blob/main/contributing.md).## License
[Apache License 2.0](https://github.com/TomerAberbach/weak-merge/blob/main/license)
This is not an official Google product.