All Projects â†’ TomerAberbach â†’ weak-merge

TomerAberbach / weak-merge

Licence: Apache-2.0 license
🔗 A module for merging WeakSets and WeakMaps.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to weak-merge

example-typescript-package
Example TypeScript Package ready to be published on npm & Tutorial / Instruction / Workflow for 2021
Stars: ✭ 71 (+255%)
Mutual labels:  npm-package
typescript-npm-package-template
Boilerplate to kickstart creating an npm package using TypeScript
Stars: ✭ 122 (+510%)
Mutual labels:  npm-package
ostrio-analytics
📊 Visitor's analytics tracking code for ostr.io service
Stars: ✭ 14 (-30%)
Mutual labels:  npm-package
retryx
Promise-based retry workflow library.
Stars: ✭ 21 (+5%)
Mutual labels:  npm-package
sdbm
SDBM non-cryptographic hash function
Stars: ✭ 43 (+115%)
Mutual labels:  npm-package
electron-remote-dashboard
Remote dashboard with a control app
Stars: ✭ 14 (-30%)
Mutual labels:  npm-package
frontatish
A React native common components kit and helper methods,find the package at this link https://www.npmjs.com/package/frontatish
Stars: ✭ 14 (-30%)
Mutual labels:  npm-package
create-xo
Add XO to your project
Stars: ✭ 41 (+105%)
Mutual labels:  npm-package
TypeScript-Library-Checklist
Your pre-launch checklist.
Stars: ✭ 19 (-5%)
Mutual labels:  npm-package
googlesheetstojson
An npm package to read Google Sheets data and convert it to JSON without publishing it to the web
Stars: ✭ 24 (+20%)
Mutual labels:  npm-package
windows-network-drive
Do network drive stuff on Microsoft Window in node
Stars: ✭ 18 (-10%)
Mutual labels:  npm-package
react-native-lightning-modal
A performant bottom modal that works using React Native Reanimated 2
Stars: ✭ 20 (+0%)
Mutual labels:  npm-package
html-to-react
A lightweight library that converts raw HTML to a React DOM structure.
Stars: ✭ 696 (+3380%)
Mutual labels:  npm-package
imrc-datetime-picker
(Improved) React component datetime picker by momentjs 📆
Stars: ✭ 21 (+5%)
Mutual labels:  npm-package
kashe
A memoization library based on weakmaps. 🤯 Sometimes cache is kashe
Stars: ✭ 60 (+200%)
Mutual labels:  weakmap
variant
Variant types in TypeScript
Stars: ✭ 147 (+635%)
Mutual labels:  union
react-native-input-prompt
A cross-platform user input prompt component for React Native with Native UI.
Stars: ✭ 45 (+125%)
Mutual labels:  npm-package
react-windows-ui
Build Windows fluent UI apps using ReactJS. Provides a set of accessible, reusable, and composable React components that make it super easy to create websites and apps.
Stars: ✭ 383 (+1815%)
Mutual labels:  npm-package
split-on-first
Split a string on the first occurrence of a given separator
Stars: ✭ 68 (+240%)
Mutual labels:  npm-package
1broker-client
Complete node.js client for 1Broker API, open sourced by @telebroker_bot for Telegram!
Stars: ✭ 19 (-5%)
Mutual labels:  npm-package

weak-merge

A module for merging WeakSets and WeakMaps.

Install

$ npm i weak-merge

Usage

import { mergeWeakSets, mergeWeakMaps } 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))
//=> false

console.log(weakSet1.has(a))
//=> true

mergedWeakSet.add(d)
console.log(mergedWeakSet.has(d))
//=> true

console.log(weakSet1.has(d))
//=> false

const 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))
//=> false

console.log(weakMap1.has(a))
//=> true

mergedWeakMap.set(a, 5)
console.log(mergedWeakMap.get(a))
//=> 5

console.log(weakMap1.get(a))
//=> 1

See the TypeScript types for more documentation.

Why?

Merging WeakSet or WeakMap instances is not trivial because they are not enumerable.

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.

For pull requests, please read the contributing guidelines.

License

Apache 2.0

This is not an official Google product.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].