Non-primitive function parameters
See original GitHub issueThe current implementation of memoize doesn’t work correctly with non-primitive function parameters.
E.g. look at this test:
it('should not overwrite non-primitive arguments', next => {
const cache = Object.create(null)
let c = memoize((key => key), { cache })
const key1 = { a: 1 }
const key2 = { a: 2 }
c(key1)
c(key2)
expect(Object.keys(cache)).to.have.length(2)
// ... cache is { '[Object object]': { a: 2 } } now
next()
});
Do you think it would make sense to print a corresponding error message when they try to pass any non-primitive ref as a parameter?
WeakMap
could be a solution for this problem (for memory leak also).
cc @stryju
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Primitive Values vs. Non-Primitive Values in JavaScript
In JavaScript, objects and functions are considered non-primitive values. You'll sometimes hear non-primitive values referred to as:.
Read more >Java Non-Primitive Data Types - W3Schools
Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot. A primitive type has always a value,...
Read more >Data Types in Java | Primitive and Non-Primitive Data Types
Primitive Data Types: A primitive data type is pre-defined by the programming language. The size and type of variable values are specified, and ......
Read more >Wrapping C++ function with arguments of non-primitive data ...
This is a real function from RocksDB C++ API . In order to use it in Go , it should be wrapped in...
Read more >Non-primitive Data Types in Java | Scaler Topics
Upon creating an object of class Demo, we have invoked two functions with some parameters to perform addition and subtraction and the result ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
yup, all correct and all currently true. Problem is I’ve used this lib quite a bit for things under the assumption only the first argument is the cache key and others are ignored. One such example would be caching GET requests through
memoize(fetch)
- passing any options object would result in a failing reference equality check for the second argument.Even with a WeakMap-implemented key setup, the following would be awkward:
to add to this, here’s a list of false-positive cache hits:
It would be “OK” assuming this would be used for functions with single-type param
on a different note,
memoize()
will not work properly for functions with more than one param