{"id":17223412,"url":"https://github.com/conduitry/memor","last_synced_at":"2025-03-25T16:23:44.636Z","repository":{"id":57294496,"uuid":"125665739","full_name":"Conduitry/memor","owner":"Conduitry","description":"More memoization. // Mirror of https://git.chor.date/Conduitry/memor","archived":false,"fork":false,"pushed_at":"2018-11-05T17:01:44.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-02T21:47:52.604Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://cndtr.dev/memor","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/Conduitry.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-03-17T20:16:58.000Z","updated_at":"2020-01-03T12:31:50.000Z","dependencies_parsed_at":"2022-09-01T13:01:42.031Z","dependency_job_id":null,"html_url":"https://github.com/Conduitry/memor","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Conduitry%2Fmemor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Conduitry%2Fmemor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Conduitry%2Fmemor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Conduitry%2Fmemor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Conduitry","download_url":"https://codeload.github.com/Conduitry/memor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245497225,"owners_count":20624969,"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":[],"created_at":"2024-10-15T04:08:16.035Z","updated_at":"2025-03-25T16:23:44.614Z","avatar_url":"https://github.com/Conduitry.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Memor: More memoization.\n\n[![npm version](https://img.shields.io/npm/v/memor.svg?style=flat-square)](https://www.npmjs.com/package/memor)\n\nMemoization, but good. Works with functions of an arbitrary and/or variable number of arguments. For arrays, regexes, dates, buffers, and POJOs, caching is done according to the *value* (and not the *identity*) of the objects. Order of keys in POJOs does not matter. For other non-primitive values, memoization still works, but the caching is done by object identity.\n\n## Requirements\n\n- [Node.js](https://nodejs.org/) 6+\n\n## Usage\n\n### `memor.memoize`\n\n```javascript\nimport { memoize } from 'memor'\n\nconst memoizedFunction = memoize(originalFunction)\n\nmemoizedFunction(/* ... */)\n```\n\n`originalFunction` can accept any number or a variable number of arguments. Re-memoizing the same function (i.e., calling `memoize(originalFunction)` elsewhere later) will share the cached values.\n\nKeying of primitives, regexes, dates, and buffers works according to their values. Any additional custom properties added to the objects will *not* be considered as part of the key. More specifically, regexes and buffers are keyed according to their `.toString()`, and dates are keyed according to their `.getTime()`.\n\nKeying of arrays (prototype `Array.prototype`), POJOs (prototype `Object.prototype`), and prototype-less objects (prototype `null`) works according to their enumerable and non-enumerable property names and symbols and their values, without regard to the order they appear.\n\nOther objects are by default simply keyed according to their identity (i.e., `===`), although this can be extended (see `memor.add`, below).\n\nTake a look at the unit tests in [`test.js`](test.js) for some specific examples of what will and will not get keyed the same way.\n\n### `memor.clear`\n\n```javascript\nimport { memoize, clear } from 'memor'\n\nconst memoizedFunction = memoize(originalFunction)\n\nmemoizedFunction(/* ... */)\n\nclear(originalFunction)\n// or\nclear(memoizedFunction)\n\nmemoizedFunction(/* ... */)\n```\n\nAll memoized values for a function can be cleared by calling `clear` on the original function or on the memoized function. These do exactly the same thing: Since all memoized copies of the same function share the same cache, clearing one clears all of them.\n\n### `memor.add`\n\n```javascript\nimport { add } from 'memor'\n\nadd(Class1, Class2, ...)\n```\n\nThis makes keying of instances of `Class1`, `Class2`, etc. work the same as arrays, POJOs, and prototype-less objects. That is, keyed according to their prototype and their enumerable and non-enumerable property names and symbols and their values, without regard to the order they appear. Note that this only sets up handling for _direct_ instances of `Class1`, etc. (i.e., those objects whose prototype is `Class1.prototype`, etc.).\n\n### `memor.addCustom`\n\n```javascript\nimport { addCustom } from 'memor'\n\naddCustom(Class1, Class2, ..., (obj, push, recurse) =\u003e { /* ... */ })\n```\n\nThis allows more customization of how instances of `Class1`, `Class2`, etc. are keyed. In general, keying objects involves converting them into a linear array of primitives and objects to use as `Map`/`WeakMap` keys. Two objects will be keyed together if and only if they are converted to arrays of the same (`===`) primitives and objects.\n\nA custom handler is implemented by writing a function that accepts three arguments: `obj` (the object to compute the representation for), `push` (a function to be called with one or more primitives or objects to append to the representation for this object), and `recurse` (a function to be called to insert the representation for another object). Take a look at the implementations of the existing handlers in [`handlers.js`](src/handlers.js) for more details on how these could work.\n\n### `memoizedFunction.original`\n\n```javascript\nimport { memoize } from 'memor'\n\nconst memoizedFunction = memoize(originalFunction)\n\nmemoizedFunction.original === originalFunction // true\n```\n\nThe original function is available as the `.original` property on the memoized function.\n## Misc\n\n- [changelog](CHANGELOG.md#readme)\n- [homepage](https://cndtr.io/memor/)\n\n## License\n\nCopyright (c) 2018 Conduitry\n\n- [MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconduitry%2Fmemor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconduitry%2Fmemor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconduitry%2Fmemor/lists"}