{"id":50383232,"url":"https://github.com/federico-paolillo/memoize","last_synced_at":"2026-05-30T13:03:38.515Z","repository":{"id":33954669,"uuid":"139140718","full_name":"federico-paolillo/memoize","owner":"federico-paolillo","description":"Memoize C# functions with ease.","archived":false,"fork":false,"pushed_at":"2022-03-01T22:13:06.000Z","size":67,"stargazers_count":23,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2026-05-29T09:22:37.043Z","etag":null,"topics":["csharp","functional","functional-programming","functions","memoization","memoize","net-standard"],"latest_commit_sha":null,"homepage":"","language":"C#","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/federico-paolillo.png","metadata":{"files":{"readme":"README.md","changelog":null,"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-06-29T11:19:31.000Z","updated_at":"2024-12-27T22:30:50.000Z","dependencies_parsed_at":"2022-08-07T23:30:58.463Z","dependency_job_id":null,"html_url":"https://github.com/federico-paolillo/memoize","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/federico-paolillo/memoize","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/federico-paolillo%2Fmemoize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/federico-paolillo%2Fmemoize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/federico-paolillo%2Fmemoize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/federico-paolillo%2Fmemoize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/federico-paolillo","download_url":"https://codeload.github.com/federico-paolillo/memoize/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/federico-paolillo%2Fmemoize/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33693027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-30T02:00:06.278Z","response_time":92,"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":["csharp","functional","functional-programming","functions","memoization","memoize","net-standard"],"created_at":"2026-05-30T13:03:37.835Z","updated_at":"2026-05-30T13:03:38.510Z","avatar_url":"https://github.com/federico-paolillo.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![MIT License](https://img.shields.io/github/license/federico-paolillo/memoize.svg?style=flat-square)](https://github.com/federico-paolillo/memoize/blob/master/LICENSE)\r\n[![Travis branch](https://img.shields.io/travis/federico-paolillo/memoize/master.svg?style=flat-square)](https://travis-ci.org/federico-paolillo/memoize)\r\n[![NuGet](https://img.shields.io/nuget/v/FP.Memoization.svg?style=flat-square)](https://www.nuget.org/packages/FP.Memoization/)\r\n\r\n# Memoizer\r\n\r\nMemoize C# functions with ease.  \r\nNo dependencies, targets [.NET Standard 1.1](https://github.com/dotnet/standard/blob/master/docs/versions/netstandard1.1.md).  \r\nAll 16 Func delegates are supported and tested using source code generation.\r\n\r\n# Why ?\r\n\r\n[From Wikipedia:](https://en.wikipedia.org/wiki/Memoization)  \r\n_In computing, memoization [...] is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again._\r\n\r\nEssentially we want to avoid executing a function again if its inputs do not change.  \r\nThis repository provides helper methods to memoize a function that can take up to 16 parameters.\r\n\r\nAnother reason for this package is that most of NuGet packages out there are outdated and do not target .NET Standard so they cannot be used with .NET Core.  \r\n\r\n# How do I use it ?\r\n\r\nAdd an `using` for `Memoization` namespace.  \r\nCall `Memoizer.Memoize(fn)` and pass it the function that you want to memoize.  \r\nYou will get back an instance of `Memoizer` which you can use to control memoization for your function.  \r\nInvoke the method `Call` from the `Memoizer` instance to execute the memoized function.  \r\n\r\nFor example:  \r\n\r\n```csharp\r\n\r\nusing Memoization;\r\n\r\n...\r\n\r\npublic string SomeFunctionThatYouWantToMemoize(string someArgs) {\r\n  ...\r\n}\r\n\r\n//Memoize the function\r\nvar memoizedFn = Memoizer.Memoize(SomeFunctionThatYouWantToMemoize);\r\n\r\n//Call the memoized function\r\nvar result = memoizedFn.Call(\"Something\");\r\n\r\n```\r\n\r\nA `Memoizer` instance can also be implicitly converted to a Func so you can use it more transparently (e.g.: with events, callbacks or whatever want a `Func` delegate).  \r\nWhat you get is essentially a reference to `Memoizer.Call` that you can still control from the `Memoizer` instance.  \r\n\r\n```csharp\r\n\r\n//Memoize the function\r\nvar memoizedFn = Memoizer.Memoize(SomeFunctionThatYouWantToMemoize);\r\n\r\n//Convert the Memoizer instance to a `Func` implictily\r\nFunc\u003cstring, string\u003e memoizedFnAsFunc = memoizedFn;\r\n\r\n```\r\n\r\nAnother thing to take into consideration is that **memoization might generate memory leaks** because the lifetime of the parameters will be as long as the memoized function instance (because parameters are stored in the memoized function instance).  \r\n\r\nIn case you want to clear any memoized parameters and results you can call `Memoizer.Reset` on a `Memoizer` instance, like so:  \r\n\r\n```csharp\r\n\r\n//Memoize the function\r\nvar memoizedFn = Memoizer.Memoize(SomeFunctionThatYouWantToMemoize);\r\n\r\n//Cleanup any memoized data\r\nmemoizedFn.Reset();\r\n\r\n```\r\n\r\nThis is useful if you wish to force evaluating the function again or clear any parameters and results stored.  \r\nNote that any custom `IEqualityComparer\u003cT\u003e` registered will **not** be removed.\r\n\r\nYou can also influence how some parameter types are compared by suppling a custom `IEqualityComparer\u003cT\u003e` for the type you want to compare using `Memoizer.WithEqualityComparer\u003cT\u003e`.  \r\n\r\n```csharp\r\n\r\n//Memoize the function\r\nvar memoizedFn = Memoizer.Memoize(SomeFunctionThatYouWantToMemoize);\r\n\r\nmemoizedFn.WithEqualityComparer\u003cT\u003e(StringComparer.OrdinalIgnoreCase);\r\n\r\n```\r\n\r\nThere aren't many safety checks in place, so you can supply as many `IEqualityComparer\u003cT\u003e` as you want, even for types that do not appear in the function signature.  \r\nIf you supply different `IEqualityComparer\u003cT\u003e` for the same type only the last one will be stored and used.  \r\n\r\nNote that this implementation does not attempt to take into consideration subclasses or interfaces, so if your type implements an interface and you supply an `IEqualityComparer\u003cT\u003e` for that interface it will not be taken into consideration during comparison. You must supply comparers for the _exact_ type that appears in the function signature.    \r\n\r\n# Tips on function memoization\r\n\r\n_Memoization works best if the parameters are immutable_.  \r\nHaving immutable parameters allows us to compare them with a simple reference equal, improving performance.\r\n\r\n_A memoized function should not have side effects._  \r\nMemoization will prevent the function from executing if the inputs are the same therefore if you depend on some side effects resulting from the function execution you will lose them.  \r\n\r\n_A memoized function should be pure._  \r\nThat means that the function result is the same if the inputs are the same, if the function was to depend on other context it would be impossible to memoize correctly as only parameters can be cached. \r\n\r\n# Limitations\r\n\r\n_The memoization mechanism does not even try to be thread safe_. Keep that in mind.  \r\n\r\nTo make sure that the memoization implementation does not use too much memory and that there is a reliable cache invalidation mechanism only the _last_ parameters used to invoke the function are recorded, if they change the function is evaluated again. Therefore it is better if you memoize only functions that you know are called many times with the same parameters.  \r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffederico-paolillo%2Fmemoize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffederico-paolillo%2Fmemoize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffederico-paolillo%2Fmemoize/lists"}