{"id":20418536,"url":"https://github.com/willnode/hashmap-with-ttl","last_synced_at":"2025-10-07T13:05:23.896Z","repository":{"id":238075283,"uuid":"795811804","full_name":"willnode/hashmap-with-ttl","owner":"willnode","description":"Hashmap with Time-to-live, as you might not need to use Redis!","archived":false,"fork":false,"pushed_at":"2024-05-04T14:54:01.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-23T03:36:30.599Z","etag":null,"topics":["nodejs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/hashmap-with-ttl","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/willnode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"willnode"}},"created_at":"2024-05-04T06:01:57.000Z","updated_at":"2024-05-04T07:20:51.000Z","dependencies_parsed_at":"2025-01-15T13:48:37.268Z","dependency_job_id":"49f6b5d5-6c40-49c9-98fa-f2a3e00db5ae","html_url":"https://github.com/willnode/hashmap-with-ttl","commit_stats":null,"previous_names":["willnode/hashmap-with-ttl"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/willnode/hashmap-with-ttl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnode%2Fhashmap-with-ttl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnode%2Fhashmap-with-ttl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnode%2Fhashmap-with-ttl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnode%2Fhashmap-with-ttl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willnode","download_url":"https://codeload.github.com/willnode/hashmap-with-ttl/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willnode%2Fhashmap-with-ttl/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278778974,"owners_count":26044259,"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","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["nodejs"],"created_at":"2024-11-15T06:33:32.684Z","updated_at":"2025-10-07T13:05:23.881Z","avatar_url":"https://github.com/willnode.png","language":"JavaScript","readme":"# `hashmap-with-ttl`\n\nA hashmap implementation with maximum capacity and time-to-live limit.\n\nThe idea is primilarily for caching, just like Redis which also has time-to-live data config but you also wish to avoid it. \n\nIn another word, if you think you need Redis for caching data, you might not need to.\n\nThe performance penalty should be negliglibe compared with using plain HashMap, as all implementation methods is O(1). We internally use linked-list structure to manage time-to-live data.\n\nThis library is implemented using ESM imports and private methods. Node 16 is required.\n\n## Usage\n\nUsing `capacity` option:\n\n```js\nimport { HashMap } from \"hashmap-with-ttl\";\n\nconst map = new HashMap({ capacity: 2 });\nmap.update(\"foo\", \"bar\");\nmap.update(\"fuu\", \"baz\");\nmap.update(\"fuu\", \"bah\"); // \"baz\" is replaced at this point\nmap.update(\"faa\", \"bal\"); // \"foo\" is gone at this point\nexpect(t.length()).toBe(2);\nexpect(t.get(\"foo\")).toBe(undefined);\nexpect(t.purge(\"fuu\")).toBe(\"bah\"); // \"fuu\" is manually purged\nexpect(t.get(\"fuu\")).toBe(undefined);\nexpect(t.length()).toBe(1);\n```\n\n\nUsing `ttl` option:\n\n```js\nimport { HashMap } from \"hashmap-with-ttl\";\n\n\nlet mockTime = 0; // In this example, we replace Date.now() \n                 // with this mocked value. In real world if you want to\n                // say, the hashmap cache should be invalid after an hour, \n               // then you set it like: new HashMap({ ttl: 3600 * 1000 })\nconst t = new HashMap({ ttl: 2000, nowFn: () =\u003e mockTime });\nexpect(t.length()).toBe(0);\nt.update(\"foo\", \"bar\");\nmockTime += 1000; // 1 second elapsed\nt.update(\"fuu\", \"baz\");\nmockTime += 1000; // 1 second elapsed\nt.update(\"faa\", \"bal\");\nmockTime += 1000; // 1 second elapsed\n\n// at this point this key is invalid\nexpect(t.get(\"foo\")).toBe(undefined);\n// but actually the data still there!\n\n// The cleanup() function erases the\n// expiring data from memory\nexpect(t.cleanup()).toBe(1); \nexpect(t.length()).toBe(2);\n// Ideally you want to call cleanup()\n// With setInterval... say every single day\n```\n\nYou can set `capacity` and `ttl` both at the same time 👍\n","funding_links":["https://github.com/sponsors/willnode"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillnode%2Fhashmap-with-ttl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillnode%2Fhashmap-with-ttl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillnode%2Fhashmap-with-ttl/lists"}