Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replaces lodash/memoize with memoize (#923)
## Description - replaces `lodash/memoize` with `memoize` ## Motivation and Context We should slowly be starting to replace lodash functions everywhere as lodash isn't really maintained anymore. Additionally `lodash/memoize` isn't the fastest memoization function around, it looks like. The chosen alternative (`memoize`, formerly known as `mem`) isn't the fastest either, but - seems to be reliably maintained - has recent updates - is decently fast (see benchmarks below) - is not too large (& doesn't ship unnecessary stuff to npm like tests, sources, bundles, benchmarks, ...) - performs decently and reliably - takes a custom resolver function (which it calls 'cacheKey'), which is a thing we regrettably need. Other candidates either have fallen out of maintenance (e.g. 10y without updates, ...), or miss features we need. <details> <summary>Speed benchmarks adopted from `memize` (who adopted from them in turn from `moize`):</summary> - run on MacOS 12.7.4 on an 2,6Ghz Quad-Core Intel Core i7 with 16Gb RAM - See https://github.com/aduth/memize/tree/master/benchmark for details - The benchmark this ran was (1) adapted to run as ESM (2) adds lodash/memoize #### Single parameter ```text ┌────────────────────┬─────────────┬──────────────────────────┬─────────────┐ │ Name │ Ops / sec │ Relative margin of error │ Sample size │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ fast-memoize │ 127,482,919 │ ± 0.28% │ 94 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ moize │ 101,448,279 │ ± 0.16% │ 94 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ memoize │ 89,864,460 │ ± 0.15% │ 96 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ memize │ 75,527,611 │ ± 1.16% │ 93 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ lodash │ 50,176,290 │ ± 0.24% │ 95 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ ramda │ 48,060,225 │ ± 0.54% │ 94 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ underscore │ 30,460,634 │ ± 0.42% │ 93 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ memoizee │ 18,648,393 │ ± 0.51% │ 93 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ moize (serialized) │ 16,516,070 │ ± 0.25% │ 92 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ memoizerific │ 9,432,079 │ ± 0.97% │ 92 │ ├────────────────────┼─────────────┼──────────────────────────┼─────────────┤ │ memoizejs │ 1,610,735 │ ± 0.28% │ 94 │ └────────────────────┴─────────────┴──────────────────────────┴─────────────┘ ``` #### Multiple parameters, only primitives ```text ┌────────────────────┬────────────┬──────────────────────────┬─────────────┐ │ Name │ Ops / sec │ Relative margin of error │ Sample size │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ moize │ 32,944,874 │ ± 3.55% │ 79 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memize │ 32,530,910 │ ± 2.19% │ 65 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memoizee │ 11,358,939 │ ± 2.89% │ 90 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memoize │ 9,380,007 │ ± 1.55% │ 88 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ moize (serialized) │ 8,536,549 │ ± 0.47% │ 90 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memoizerific │ 5,622,282 │ ± 1.19% │ 91 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ lodash │ 5,393,388 │ ± 0.73% │ 92 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memoizejs │ 1,101,890 │ ± 1.10% │ 92 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ fast-memoize │ 929,638 │ ± 0.22% │ 95 │ └────────────────────┴────────────┴──────────────────────────┴─────────────┘ ``` #### Multiple parameters, some parameters contain objects ```plaintext ┌────────────────────┬────────────┬──────────────────────────┬─────────────┐ │ Name │ Ops / sec │ Relative margin of error │ Sample size │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memize │ 30,529,321 │ ± 0.97% │ 89 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ moize │ 28,842,916 │ ± 0.77% │ 91 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memoizee │ 10,445,895 │ ± 1.02% │ 92 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memoize │ 9,349,388 │ ± 0.50% │ 93 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memoizerific │ 6,107,483 │ ± 1.29% │ 92 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ lodash │ 5,029,315 │ ± 0.36% │ 92 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ memoizejs │ 808,879 │ ± 2.71% │ 94 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ moize (serialized) │ 796,306 │ ± 1.73% │ 88 │ ├────────────────────┼────────────┼──────────────────────────┼─────────────┤ │ fast-memoize │ 650,625 │ ± 3.18% │ 88 │ └────────────────────┴────────────┴──────────────────────────┴─────────────┘ ``` </details> ## How Has This Been Tested? - [x] green ci ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] Documentation only change - [x] Refactor (non-breaking change which fixes an issue without changing functionality) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change)
- Loading branch information