caiogondim / fast-memoize.js

:rabbit2: Fastest possible memoization library
https://npm.im/fast-memoize
MIT License
2.58k stars 86 forks source link

Default argument breaks correct memoization #64

Closed alexkrautmann closed 6 years ago

alexkrautmann commented 6 years ago

I am running this in webpack and have tried on requirebin.

const fastMemoize = require('fast-memoize');

const memoConcat = fastMemoize((arg1, arg2 = 'default') => {
  // fastMemoize does not work correctly with default arguments
  const value = arg1 + '-' + arg2;
  return value;
});

console.log(memoConcat('foo', 'bar')); // -> incorrectly returns 'foo-default'
planttheidea commented 6 years ago

This is a known limitation, as variadic arguments are not fully supported. Internally function.length is used, and default arguments prevent accurate determinations with this field.

alexkrautmann commented 6 years ago

@planttheidea Thanks, good to know. I imagine getting past this might bloat the package, but it is a bit unfortunate as I am weary to include this in a project where I might have to explain this on every usage via comment. The risk is that other devs might not realize this limitation and bugs could surface.