medikoo / memoizee

Complete memoize/cache solution for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not working, on using default parameters in memoized function

saggiyogesh opened this issue · comments

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).

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)

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

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) -> a988fe3