medikoo / memoizee

Complete memoize/cache solution for JavaScript
ISC License
1.73k stars 61 forks source link

Not working, on using default parameters in memoized function #95

Closed saggiyogesh closed 6 years ago

saggiyogesh commented 6 years ago
var memoize = require('memoizee')
const fn1 = function (a = {}) {
  a.k = 1;
  return a;
};

const fn = memoize(fn1);
console.log(fn(), fn1()); // { k: 1 } { k: 1 }

const o = { s: 2 };
console.log(fn(o), fn1(o)); // { k: 1 } { s: 2, k: 1 }

In the above snippet, fn is the memoized version of fn1 which is having a default parameter. When an object is passed in the arguments, fn1 still returning results of default arguments, ignoring the passed argument (o).

medikoo commented 6 years ago

This actually works as intended. memoizee by default only takes into account mandatory arguments (resolved on basis of function's length).

To force memoizee to take into account optional arguments (ones with default values) you need to pass length option (in above case length: 1, as for your function length === 0)

saggiyogesh commented 6 years ago

Yeah thanks, it's working now after giving length option. Please update the same in the documentation.

medikoo commented 6 years ago

Please update the same in the documentation.

Documentation was actually quite clear on that: https://github.com/medikoo/memoizee#arguments-length. Still I added a note about how default parameters behave (language feature) -> https://github.com/medikoo/memoizee/commit/a988fe35ee4533858e88b64829e4dc06f79efe53