{"name":"memoization-registry","repository":{"type":"git","url":"git+https://github.com/fabiospampinato/memoization-registry.git"},"description":"A generalized multi-key memoization solution that does not leak memory.","license":"MIT","version":"1.0.2","type":"module","main":"dist/index.js","exports":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"clean":"tsex clean","compile":"tsex compile","compile:watch":"tsex compile --watch","test":"node --expose-gc test/index.js","prepublishOnly":"npm run clean && npm run compile && npm run test"},"keywords":["memoization","finalization","registry","memory","leak"],"dependencies":{"mild-map":"^1.1.2"},"devDependencies":{"fava":"^0.3.4","tsex":"^4.0.2","typescript":"^5.7.3"},"_id":"memoization-registry@1.0.2","gitHead":"eb2b34273093423921ba372a38482111dfcc06d5","bugs":{"url":"https://github.com/fabiospampinato/memoization-registry/issues"},"homepage":"https://github.com/fabiospampinato/memoization-registry#readme","_nodeVersion":"18.19.0","_npmVersion":"10.2.3","dist":{"integrity":"sha512-HLYn0FWqDFOeghQtuDhHySUtphPWYHo63GLSSapaN2n9vXI6/zry6lJ+AvV9ZC34/w6CbscRnLwvtL89tnDr5Q==","shasum":"de4efc55d784dd9a46e78216e949342cbd3c985d","tarball":"http://registry-npm.ywwl.com/memoization-registry/download/memoization-registry-1.0.2.tgz","fileCount":9,"unpackedSize":9949,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDf6gIbMXemCqDCTADbJFfYjpVUMdWUDQmY37tqQRuxDwIhAN5C4YjupUZC9Y74uLbz1YiR1wTdYH/QWur4q4bNfaRu"}],"size":3467,"noattachment":false,"key":"/memoization-registry/-/memoization-registry-1.0.2.tgz"},"_npmUser":{"name":"fabiospampinato","email":"spampinabio@gmail.com"},"directories":{},"maintainers":[{"name":"fabiospampinato","email":"spampinabio@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoization-registry_1.0.2_1737901849703_0.8188389380414716"},"_hasShrinkwrap":false,"readme":"# Memoization Registry\n\nA generalized multi-key memoization solution that does not leak memory.\n\n## Features\n\n- It supports memoizing on multiple arbitrary keys.\n- It supports memoizing things other than function calls too.\n- It doesn't hold strong references to memoized values nor memoization keys, unless they are primitives.\n\n## Install\n\n```sh\nnpm install memoization-registry\n```\n\n## Usage\n\nMaybe the best way to explain what this allows for is with an example, how would you write a \"Magic\" class in such a way that all the following assertions succeeded?\n\n```ts\nclass Foo extends Magic {}\n\nconst foo1 = new Foo ( 'some-id' );\nconst foo2 = new Foo ( 'other-id' );\nconst foo3 = new Foo ( 'some-id' );\n\nconsole.assert ( foo1 !== foo2 );\nconsole.assert ( foo2 !== foo3 );\nconsole.assert ( foo1 === foo3 );\n\nclass Bar extends Foo {}\n\nconst bar1 = new Bar ( 'some-id' );\nconst bar2 = new Bar ( 'other-id' );\nconst bar3 = new Bar ( 'some-id' );\n\nconsole.assert ( foo1 !== bar1 );\nconsole.assert ( foo2 !== bar2 );\nconsole.assert ( foo3 !== bar3 );\n\nconsole.assert ( bar1 !== bar2 );\nconsole.assert ( bar2 !== bar3 );\nconsole.assert ( bar1 === bar3 );\n```\n\nIt may seem borderline impossible to simply make that work without any problems -- basically we want to automatically memoize some class instances, but the thing to memoize is not a regular function call, and we don't want to hold strong references to the memoized values, nor the memoization keys, as that will cause a memory leak.\n\nHere's a solution, enabled by this powerful library:\n\n```ts\nimport MemoizationRegistry from 'memoization-registry';\n\nconst registry = new MemoizationRegistry<[Function, string], Magic> ();\n\nclass Magic {\n  constructor ( id: string ) {\n    const keys = [new.target, id];\n    const factory = () => this;\n    return registry.register ( keys, factory );\n  }\n}\n\nclass Foo extends Magic {}\n\nconst foo1 = new Foo ( 'some-id' );\nconst foo2 = new Foo ( 'other-id' );\nconst foo3 = new Foo ( 'some-id' );\n\nconsole.assert ( foo1 !== foo2 );\nconsole.assert ( foo2 !== foo3 );\nconsole.assert ( foo1 === foo3 );\n\nclass Bar extends Foo {}\n\nconst bar1 = new Bar ( 'some-id' );\nconst bar2 = new Bar ( 'other-id' );\nconst bar3 = new Bar ( 'some-id' );\n\nconsole.assert ( foo1 !== bar1 );\nconsole.assert ( foo2 !== bar2 );\nconsole.assert ( foo3 !== bar3 );\n\nconsole.assert ( bar1 !== bar2 );\nconsole.assert ( bar2 !== bar3 );\nconsole.assert ( bar1 === bar3 );\n```\n\n## License\n\nMIT © Fabio Spampinato\n","publish_time":1737901849899,"_cnpm_publish_time":1737901849899,"dist-tags":{"latest":"1.0.2"}}