{"id":17096461,"url":"https://github.com/ramiel/memoizy","last_synced_at":"2026-03-09T12:06:27.750Z","repository":{"id":33521757,"uuid":"157921894","full_name":"ramiel/memoizy","owner":"ramiel","description":"Memoization helper","archived":false,"fork":false,"pushed_at":"2023-01-06T01:36:57.000Z","size":1537,"stargazers_count":2,"open_issues_count":11,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-05T08:58:12.797Z","etag":null,"topics":["cache","memoization-helper","memoize"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/memoizy","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/ramiel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-16T21:13:11.000Z","updated_at":"2022-10-17T23:58:48.000Z","dependencies_parsed_at":"2023-01-15T01:16:42.167Z","dependency_job_id":null,"html_url":"https://github.com/ramiel/memoizy","commit_stats":null,"previous_names":["ramiel/m"],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/ramiel/memoizy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramiel%2Fmemoizy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramiel%2Fmemoizy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramiel%2Fmemoizy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramiel%2Fmemoizy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ramiel","download_url":"https://codeload.github.com/ramiel/memoizy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ramiel%2Fmemoizy/sbom","scorecard":{"id":760736,"data":{"date":"2025-08-11","repo":{"name":"github.com/ramiel/memoizy","commit":"839e2529ca726daad02a80ef76df285c450b9d67"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.7,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Code-Review","score":0,"reason":"Found 1/26 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 7 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"28 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-968p-4wvh-cqc8","Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92","Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq","Warn: Project is vulnerable to: GHSA-fjxv-7rqg-78g4","Warn: Project is vulnerable to: GHSA-ww39-953v-wcq6","Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj","Warn: Project is vulnerable to: GHSA-896r-f27r-55mw","Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h","Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9","Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h","Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9","Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp","Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6","Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-52f5-9888-hmc6","Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v","Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3","Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7","Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-22T23:23:45.420Z","repository_id":33521757,"created_at":"2025-08-22T23:23:45.421Z","updated_at":"2025-08-22T23:23:45.421Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30294777,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-09T11:12:22.024Z","status":"ssl_error","status_checked_at":"2026-03-09T11:10:54.577Z","response_time":61,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","memoization-helper","memoize"],"created_at":"2024-10-14T14:45:45.274Z","updated_at":"2026-03-09T12:06:27.718Z","avatar_url":"https://github.com/ramiel.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Memoizy\n\n[![npm version](https://badge.fury.io/js/memoizy.svg)](https://badge.fury.io/js/memoizy)\n[![CircleCI](https://circleci.com/gh/ramiel/memoizy.svg?style=svg)](https://circleci.com/gh/ramiel/memoizy)\n[![Coverage Status](https://coveralls.io/repos/github/ramiel/memoizy/badge.svg?branch=master)](https://coveralls.io/github/ramiel/memoizy?branch=master)\n\nThis is a memoization helper that let you memoize and also have the following features\n\n- ⏰ Max age: discard memoized value after a configurable amount of time \n- ♻️ Custom cache: drop-in your favorite cache or your own implementation\n- 🗝 Custom cache key: decide how to build the cache keys\n- 🧹 Clear and delete: delete all the memoized values or just one for a specific arguments set \n- ❓ Conditional memoization: memoize the result only if you like it. It works with async code too\n- 🧪 Fully tested\n- 👶 Small size and no dependencies\n- 👣 Small footprint\n- \u0026nbsp;**λ**\u0026nbsp;\u0026nbsp; FP style available\n\n## Usage\n\n### Basic\n\nMemoize the return value of a function\n\n```js\nconst memoizy = require('memoizy');\n\nconst fact = (n) =\u003e {\n  if(n === 1) return n;\n  return n * fact(n - 1);\n}\n\nconst memoizedFact = memoizy(fact);\nmemoizedFact(3); // 6\nmemoizedFact(3); // the return value is always 6 but\n                 // the factorial is not computed anymore\n```\n\nIf you're using ES6 or typescript, you can import it with\n\n```ts\nimport { memoizy } from 'memoizy';\n```\n\n## API\n\nThe memoize function is defined like that:\n\n`memoizy(fn, options)`\n\nwhere `fn` is the function to memoize    \nand `options` is an (optional) object with the following keys:\n\n- `maxAge`: Tell how much time the value must be kept in memory, in milliseconds. 0 or negative values mean forever. Default: Infinity\n- `cache`: Specify a different cache to be used. It's a function that returns a new cache that must have the same interface as [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map). Default `() =\u003e new Map()`\n- `cacheKey`: Function to build the cache key given the arguments.\n- `valueAccept`: Function in the form `(err, value) =\u003e true/false`. It receive an error (if any) and the memoized value and return true/false. If false is returned, the value is discarded. If the memoized function returns a promise, the resolved value (or the rejection error) is passed to the function. Default null (all values accepted)\n\n`memoizy` return the memoized function with two other properties: `delete` and `clear`\n\n- `delete`: is a function that takes the same arguments as the original function and delete the entry for those arguments\n- `clear`: is a function that deletes all the cached entries\n\n## Recipes\n\n### Expire data\n\n```js\nconst memoize = require('memoizy');\n\nconst double = memoize(a =\u003e a * 2, {maxAge: 2000});\n\ndouble(2); // 4\ndouble(2); // returns memoized 4\n// wait 2 seconds, memoized value has been discarded\ndouble(2); // Original function is called again and 4 is returned. The value is memoized for other 2 seconds\n```\n\n### Discard rejected promises\n\n```js\nconst memoize = require('memoizy');\n\nconst originalFn = async (a) =\u003e {\n  if(a \u003e 10) return 100;\n  throw new Error('Value must be more then 10');\n}\nconst memoized = memoize(originalFn, {valueAccept: (err, value) =\u003e !err});\n\nawait memoized(1); // throw an error and the value is not memoized\nawait memoized(15); // returns 100 and the value is memoized\n```\n\n\n### Discard some values\n\n```js\nconst memoize = require('memoizy');\n\nconst originalFn = (a) =\u003e {\n  if(a \u003e 10) return true;\n  return false;\n}\n// Tell to ignore the false value returned\nconst memoized = memoize(originalFn, {valueAccept: (err, value) =\u003e value === true});\n\nawait memoized(1); // ignores the result since it's false\nawait memoized(15); // returns true and it's memoized\n```\n\n### Delete and Clear\n\n```js\nconst memoize = require('memoizy');\n\nconst sum = (a, b) =\u003e a + b;\n\nconst memSum = memoize(sum);\n\nmemSum(5, 4); // returns 9;\nmemSum(1, 3); // returns 4;\nmemSum.delete(5,4); // remove the entry for the memoized result 9\nmemSum(1, 3); // returns 4 without comupting the sum\nmemSum(5, 4); // returns 9 re-computing the sum and memoize it again\n\nmemSum.clear(); // All values are now cleared and \n                // the cache for this memoized function is empty\n```\n\n### Different cache\n\nYou can use another cache implementation if you desire. The only constraint is that it must implement\nthe methods `has`, `get`, `set`, `delete` and, optionally, `clear`.    \nIf the cache doesn't support clear, it's up to you not to call it. In case an error is thrown.\n\n**NOTE**: If you plan to use a WeakMap, remember that clear is not available in all the implementations.    \nLook [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap#Implementing_a_WeakMap-like_class_with_a_.clear()_method) for a way to use a weak map with clear implementd, as cache.\n\n```js\nconst memoizy = require('memoizy');\n\n const AlternativeCacheFactory = () =\u003e {\n  let data = {};\n  return {\n    has(key) { return key in data; },\n    get(key) { return data[key]; },\n    set(key, value) { data[key] = value; },\n    delete(key) { delete data[key]; },\n    clear() { data = {}; },\n  };\n};\nconst fn = a =\u003e a * 2;\nconst memFn = memoizy(fn, {cache: AlternativeCacheFactory});\n```\n\n## Use WeakMap as cache\n\nLet's see how to use a WeakMap, without implementing the optional clear.\n\n```js\nconst fn = obj =\u003e ({ ...obj, date: new Date() });\nconst memFn = memoizer(fn, {\n  // Specify a cache factory that returns a new WeakMap\n  cache: () =\u003e new WeakMap(), \n  // A WeakMap only accept non-primitive values as key.\n  // Let's change the way the key is created\n  // In this case just return the first parameter. \n  // Note that this works for weakMap caches only\n  cacheKey: obj =\u003e obj\n});\n```\n\n## Use Redis (or any cache that can handle data expiration)\n\nYour cache may have the ability to handle expiration by itself, for example Redis can do it.\nIn this case you don't want memoizy to handle expiration internally just because it's more performant\nto let Redis do the job. You can specify `cacheHandlesExpiration = true` in the options. \nThe only difference is that the cache you provide must expect the expiration time in milliseconds \nas third parameter of the `set` method. Here below an example with ioredis\n\n```js\nconst memoizy = require('memoizy');\nconst Redis = require(\"ioredis\");\nconst redis = new Redis({\n  // ...redis options\n});\n\nconst redisCache = {\n  get: redis.get.bind(redis),\n  clear: redis.flushdb.bind(redis),\n  delete: redis.del.bind(redis),\n  set: (key, value, expiration) =\u003e {\n    if(expiration) {\n      return redis.set(key, value, 'PX', expiration);\n    } else {\n      return redis.set(key, value);\n    }\n  }\n};\n/**\n * Function memoized with redis must return a promise because \n * redis lookup is asynchronous by nature\n **/\nconst fn = async (a,b) =\u003e a * b;\n\nconst memoizedMultiplyOnRedis = memoizy(fn, {\n  maxAge: 5000,\n  cache: () =\u003e redisCache,\n  cacheHandlesExpiration: true,\n})\n```\n\n## FP style alternative\n\nThis library offers a variant which is handy if you develop in functional programming style.\n\nIt has the same features and the following differences:\n- the order of the parameters is inverted `memoizy(options, fn)`\n- the `memoizy` function is curried\n\nAn example\n\n```js\nconst memoizy = require('memoizy').fp;\n\n// since it is curried, we can pass just options and a new function will be returned\nconst memoizeFor5Seconds = memoizy({maxage: 5 * 1000});\n\nconst double = a =\u003e a * 2;\nconst triple = a =\u003e a * 3;\n\n// Now we can memoize both functions with the same options\nconst memoizedDouble = memoizeFor5Seconds(double);\nconst memoizedTriple = memoizeFor5Seconds(triple);\n```\n\nIf you're using ES6 or typescript, you can import with\n\n```ts\nimport { fp as memoizy } from 'memoizy';\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framiel%2Fmemoizy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Framiel%2Fmemoizy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Framiel%2Fmemoizy/lists"}