Giter VIP home page Giter VIP logo

weak-merge's Introduction

weak-merge

A module for merging WeakSets and WeakMaps.

Install

$ npm i weak-merge

Usage

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))
//=> 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 License 2.0

This is not an official Google product.

weak-merge's People

Contributors

tomeraberbach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.