Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replaces lodash/memoize with memoize #923

Merged
merged 1 commit into from
Mar 17, 2024
Merged

Conversation

sverweij
Copy link
Owner

@sverweij sverweij commented Mar 17, 2024

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.

Speed benchmarks adopted from `memize` (who adopted from them in turn from `moize`):

Single parameter

┌────────────────────┬─────────────┬──────────────────────────┬─────────────┐
│ 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

┌────────────────────┬────────────┬──────────────────────────┬─────────────┐
│ 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

┌────────────────────┬────────────┬──────────────────────────┬─────────────┐
│ 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          │
└────────────────────┴────────────┴──────────────────────────┴─────────────┘

How Has This Been Tested?

  • green ci

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Documentation only change
  • 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)

Checklist

  • 📖

    • My change doesn't require a documentation update, or ...
    • it does and I have updated it
  • ⚖️

    • The contribution will be subject to The MIT license, and I'm OK with that.
    • The contribution is my own original work.
    • I am ok with the stuff in CONTRIBUTING.md.

Copy link

codeclimate bot commented Mar 17, 2024

Code Climate has analyzed commit 39a4942 and detected 0 issues on this pull request.

View more on Code Climate.

Copy link

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/memoize@10.0.0 None +1 26.5 kB sindresorhus

View full report↗︎

@sverweij sverweij merged commit c9d8b97 into main Mar 17, 2024
10 checks passed
@sverweij sverweij deleted the refactor/use-memoize branch March 17, 2024 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant