{"id":16720360,"url":"https://github.com/tomokimiyauci/memo","last_synced_at":"2025-10-08T17:55:31.667Z","repository":{"id":173805830,"uuid":"650520394","full_name":"TomokiMiyauci/memo","owner":"TomokiMiyauci","description":"Memoization tools, TC39 proposal-function-memo implementation","archived":false,"fork":false,"pushed_at":"2024-07-08T04:21:10.000Z","size":63,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-26T19:04:12.421Z","etag":null,"topics":["cache","cache-map","memo","memoization","memoize","proposal-function-memo","tc39"],"latest_commit_sha":null,"homepage":"https://deno.land/x/memoization","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TomokiMiyauci.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-07T08:41:08.000Z","updated_at":"2023-06-17T08:19:47.000Z","dependencies_parsed_at":"2023-06-28T14:30:16.340Z","dependency_job_id":null,"html_url":"https://github.com/TomokiMiyauci/memo","commit_stats":null,"previous_names":["tomokimiyauci/memo"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/TomokiMiyauci/memo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomokiMiyauci%2Fmemo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomokiMiyauci%2Fmemo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomokiMiyauci%2Fmemo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomokiMiyauci%2Fmemo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TomokiMiyauci","download_url":"https://codeload.github.com/TomokiMiyauci/memo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TomokiMiyauci%2Fmemo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263108192,"owners_count":23414977,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","cache-map","memo","memoization","memoize","proposal-function-memo","tc39"],"created_at":"2024-10-12T22:06:51.083Z","updated_at":"2025-10-08T17:55:31.597Z","avatar_url":"https://github.com/TomokiMiyauci.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# memo\n\n[![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno)](https://deno.land/x/memoization)\n[![deno doc](https://doc.deno.land/badge.svg)](https://deno.land/x/memoization?doc)\n[![GitHub release (latest by date)](https://img.shields.io/github/v/release/TomokiMiyauci/memo)](https://github.com/TomokiMiyauci/memo/releases)\n[![codecov](https://codecov.io/github/TomokiMiyauci/memo/branch/main/graph/badge.svg)](https://codecov.io/gh/TomokiMiyauci/memo)\n[![License](https://img.shields.io/github/license/TomokiMiyauci/memo)](LICENSE)\n\n[![test](https://github.com/TomokiMiyauci/memo/actions/workflows/test.yaml/badge.svg)](https://github.com/TomokiMiyauci/memo/actions/workflows/test.yaml)\n[![NPM](https://nodei.co/npm/@miyauci/memo.png?mini=true)](https://nodei.co/npm/@miyauci/memo/)\n[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg)](https://github.com/RichardLitt/standard-readme)\n[![semantic-release: angular](https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)\n\nMemoization tools, TC39\n[proposal-function-memo](https://github.com/tc39/proposal-function-memo)\nimplementation.\n\n## Table of Contents \u003c!-- omit in toc --\u003e\n\n- [Install](#install)\n- [Usage](#usage)\n  - [Custom cache](#custom-cache)\n  - [Keying](#keying)\n  - [Instantiation caching](#instantiation-caching)\n  - [Polyfill](#polyfill)\n- [API](#api)\n- [Contributing](#contributing)\n- [License](#license)\n\n## Install\n\ndeno.land:\n\n```ts\nimport * as mod from \"https://deno.land/x/memoization/mod.ts\";\n```\n\nnpm:\n\n```bash\nnpm i @miyauci/memo\n```\n\n## Usage\n\nReturns the proxy function whose call is monitored. It calls at most once for\neach given arguments.\n\n```ts\nimport { memo } from \"https://deno.land/x/memoization/mod.ts\";\n\nfunction f(x: number): number {\n  console.log(x);\n  return x * 2;\n}\n\nconst fMemo = memo(f);\nfMemo(3); // Prints 3 and returns 6.\nfMemo(3); // Does not print anything. Returns 6.\nfMemo(2); // Prints 2 and returns 4.\nfMemo(2); // Does not print anything. Returns 4.\nfMemo(3); // Does not print anything. Returns 6.\n```\n\nEither version would work with recursive functions:\n\n```ts\nimport { memo } from \"https://deno.land/x/memoization/mod.ts\";\n\nconst fib = memo((num: number): number =\u003e {\n  if (num \u003c 2) return num;\n\n  return fib(num - 1) + fib(num - 2);\n});\n\nfib(1000);\n```\n\n### Custom cache\n\nTo control the cache, specify `cache`.\n\nThe cache must implement the following interfaces:\n\n```ts\ninterface MapLike\u003cK, V\u003e {\n  get(key: K): V | undefined;\n  has(key: K): boolean;\n  set(key: K, value: V): void;\n}\n```\n\nBy default, an unlimited cache is used by `WeakMap`.\n\n```ts\nimport { type MapLike, memo } from \"https://deno.land/x/memoization/mod.ts\";\n\ndeclare const lruCache: MapLike\u003cobject, unknown\u003e;\ndeclare const fn: () =\u003e unknown;\n\nconst $fn = memo(fn, lruCache);\n```\n\nSee TC39\n[proposal-policy-map-set](https://github.com/tc39/proposal-policy-map-set) and\nits [implementation](https://github.com/TomokiMiyauci/cache-mapset).\n\n### Keying\n\nCache keys are represented by\n[composite keys](https://github.com/tc39/proposal-richer-keys/tree/master/compositeKey).\n\nThe composite keys are passed several elements for the key, called components.\n\nThe components are as follows:\n\n- target function\n- this arg(`this`)\n- new target(`new.target`)\n- args\n\nOf these, target function is used to identify a unique function. The target\nfunction is not used to identify a unique function, since the composite key is a\nglobal registry. For more information, see\n[FAQ: What scope is the idempotentcy?](https://github.com/tc39/proposal-richer-keys/tree/master/compositeKey#what-scope-is-the-idempotentcy)\n\nAlso, composite key employs the\n[same-value-zero](https://tc39.es/ecma262/#sec-samevaluezero) algorithm to\nverify the equivalence of each component.\n\nYou can modify the args component through the `keying` callback.\n\n```ts\nimport { type MapLike, memo } from \"https://deno.land/x/memoization/mod.ts\";\n\ndeclare const respond: (request: Request) =\u003e Response;\n\nconst $respond = memo(\n  respond,\n  undefined,\n  ([request]) =\u003e [request.method, request.url],\n);\n```\n\nCurrently, only the args component can be modified. This is being discussed in\n[#4 (comment)](https://github.com/tc39/proposal-function-memo/issues/4#issuecomment-1083552333)\nand it is not clear how this arg and new target should be handled.\n\n### Instantiation caching\n\nCaching of instantiation is also supported. Calls to constructor functions with\nthe `new` operator are cacheable based on their arguments.\n\n```ts\nimport { memo } from \"https://deno.land/x/memoization/mod.ts\";\nimport { assert } from \"https://deno.land/std/testing/asserts.ts\";\n\nassert(new Error() !== new Error());\n\nconst $Error = memo(Error);\n\nassert(new $Error() === new $Error());\nassert($Error(\"test\") === $Error(\"test\"));\n\nassert(new $Error() !== $Error());\nassert(new $Error() !== new $Error(\"test\"));\n```\n\n### Polyfill\n\nPolyfill affects the global object. You must be very careful when using it.\n\n```ts\nimport \"https://deno.land/x/memoization/polyfill.ts\";\n\nconst fib = ((num: number): number =\u003e {\n  if (num \u003c 2) return num;\n\n  return fib(num - 1) + fib(num - 2);\n}).memo();\n\nfib(1000);\n```\n\n## API\n\nSee [deno doc](https://deno.land/x/memoization?doc) for all APIs.\n\n## Contributing\n\nSee [CONTRIBUTING.md](CONTRIBUTING.md)\n\n## License\n\n[MIT](LICENSE) © 2023 Tomoki Miyauchi\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomokimiyauci%2Fmemo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomokimiyauci%2Fmemo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomokimiyauci%2Fmemo/lists"}