Skip to content

Commit

Permalink
Cache function reference when it's a single argument (#35)
Browse files Browse the repository at this point in the history
Co-Authored-By: Sindre Sorhus <[email protected]>
  • Loading branch information
fregante and sindresorhus committed May 14, 2019
1 parent 1b306a2 commit 10f13c0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare namespace mem {
readonly maxAge?: number;

/**
Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array.
Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key (if it's a `function`, its reference will be used as key), otherwise it's all the function arguments JSON stringified as an array.
You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.
*/
Expand Down
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ const defaultCacheKey = (...arguments_) => {

if (arguments_.length === 1) {
const [firstArgument] = arguments_;
if (
firstArgument === null ||
firstArgument === undefined ||
(typeof firstArgument !== 'function' && typeof firstArgument !== 'object')
) {
const isObject = typeof firstArgument === 'object' && firstArgument !== null;
const isPrimitive = !isObject;
if (isPrimitive) {
return firstArgument;
}
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Milliseconds until the cache expires.

Type: `Function`

Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key, otherwise it's all the function arguments JSON stringified as an array.
Determines the cache key for storing the result based on the function arguments. By default, if there's only one argument and it's a [primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive), it's used directly as a key (if it's a `function`, its reference will be used as key), otherwise it's all the function arguments JSON stringified as an array.

You could for example change it to only cache on the first argument `x => JSON.stringify(x)`.

Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ test('memoize', t => {
t.is(memoized('foo', 'bar'), 2);
t.is(memoized('foo', 'bar'), 2);
t.is(memoized('foo', 'bar'), 2);
t.is(memoized(1), 3);
t.is(memoized(1), 3);
t.is(memoized(null), 4);
t.is(memoized(null), 4);
t.is(memoized(undefined), 5);
t.is(memoized(undefined), 5);
t.is(memoized(fixture), 6);
t.is(memoized(fixture), 6);
t.is(memoized(true), 7);
t.is(memoized(true), 7);

// Ensure that functions are stored by reference and not by "value" (e.g. their `.toString()` representation)
t.is(memoized(() => i++), 8);
t.is(memoized(() => i++), 9);
});

test('memoize with multiple non-primitive arguments', t => {
Expand Down

0 comments on commit 10f13c0

Please sign in to comment.