{"id":21933097,"url":"https://github.com/starryinternet/map-memo","last_synced_at":"2026-03-08T07:32:02.010Z","repository":{"id":57162521,"uuid":"59963565","full_name":"StarryInternet/map-memo","owner":"StarryInternet","description":"Generic memoization with Map and WeakMap","archived":false,"fork":false,"pushed_at":"2020-02-12T15:40:02.000Z","size":12,"stargazers_count":10,"open_issues_count":1,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-04-24T20:26:20.696Z","etag":null,"topics":["javascript","memoization","memoize","weakmap"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/StarryInternet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-29T20:58:17.000Z","updated_at":"2023-03-05T21:13:14.000Z","dependencies_parsed_at":"2022-09-10T09:40:51.162Z","dependency_job_id":null,"html_url":"https://github.com/StarryInternet/map-memo","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StarryInternet%2Fmap-memo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StarryInternet%2Fmap-memo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StarryInternet%2Fmap-memo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StarryInternet%2Fmap-memo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StarryInternet","download_url":"https://codeload.github.com/StarryInternet/map-memo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227011024,"owners_count":17716674,"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":["javascript","memoization","memoize","weakmap"],"created_at":"2024-11-29T00:07:45.817Z","updated_at":"2026-03-08T07:32:01.974Z","avatar_url":"https://github.com/StarryInternet.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# map-memo\n\n[![Build Status](https://travis-ci.org/StarryInternet/map-memo.svg?branch=master)](https://travis-ci.org/StarryInternet/map-memo)\n\nGeneric memoization using `Map` and `WeakMap`.\n\n---\n\n### Installing\n\n```\nnpm install --save @starryinternet/map-memo\n```\n\n---\n\n### What/Why?\n\nMemoization in JavaScript has typically been limited to arguments\nwith primitive values, or by utilizing hacks such as stringifying objects.\n\nBy storing arguments using a series of nested cache objects backed by `Map`\nand `WeakMap`, `map-memo` is able to memoize functions with *any*\nargument types.\n\n---\n\n### Example\n\n```js\n'use strict';\n\nconst memoize = require('@starryinternet/map-memo');\n\nfunction loop( fn, n ) {\n  let v;\n\n  for ( let i = 0; i \u003c n; ++i ) {\n    v = fn( i );\n  }\n\n  return v;\n}\n\nlet mem = memoize( loop );\n\nconsole.log( mem( Math.sqrt, 1e9 ) ); // slow\nconsole.log( mem( Math.sqrt, 1e9 ) ); // fast!\n```\n\n### Example with expire time in milliseconds\n\n```js\n'use strict';\n\nconst memoize = require('@starryinternet/map-memo');\n\nfunction getRandom() {\n  return Math.random();\n}\n\nlet mem = memoize( getRandom, { ttl: 1000 } );\nconsole.log( mem() );\nconsole.log( mem() ); // Same value as above\n\nsetTimeout( function() {\n  console.log( mem() ); // Different value\n}, 1001 );\n```\n\n### Example with asynchronous function\n\n```js\n'use strict';\n\nconst memoize = require('@starryinternet/map-memo');\n\nfunction loopAsync( fn, n ) {\n  return new Promise( ( resolve, reject ) =\u003e {\n    let v;\n\n    for ( let i = 0; i \u003c n; ++i ) {\n      v = fn( i );\n    }\n\n    resolve( v );\n  });\n}\n\nlet mem = memoize( loopAsync );\n\nmem( Math.sqrt, 1e9 ).then( result =\u003e {\n  console.log( result ); // slow\n  mem( Math.sqrt, 1e9 ).then( console.log ); // fast!\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarryinternet%2Fmap-memo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarryinternet%2Fmap-memo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarryinternet%2Fmap-memo/lists"}