caiogondim / fast-memoize.js

:rabbit2: Fastest possible memoization library

Home Page:https://npm.im/fast-memoize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doesn't work with inputs that have default value declaration.

ckwong90 opened this issue · comments

for example:

const foo = (bool, num = 5) => bool ? {num} : {def: "default"}
expect(foo.length).toEqual(2) //Error: foo.length === 1 instead of 2

const memFoo = memoize(foo);
expect(memFoo(true, 5) === memFoo(true, 6)).toBe(false) //Error is true
expect(memFoo(true) === memFoo(true, 6)).toBe(false) //Error is true

Maybe add this to the limitation section in docs with the workaround

const foo = (bool, num) => {
   num = num || 5; //initialize default value here
   return bool ? {num} : {def: "default"}
}

Classic:

We've discussed this extensively before and there doesn't seem to be many plausible use cases for the function length property.

https://esdiscuss.org/topic/function-length-and-default-parameters

There are even worse cases: https://medium.com/@robinpokorny/function-length-property-is-not-to-be-trusted-27c3b2d468d5

If using default args you should enforce the variadic strategy with:

const memFoo = memoize(foo, { strategy: memoize.strategies.variadic })

This should definitely be mentioned in the 'Gotchas' section of the README.