Comments (6)
from nano-memoize.
For single object arguments, when not providing a serializer, the lib defaults to using a weakmap.
Weakmaps compare keys by reference.
You're passing a different object reference in the calls you make to your memoized function.
So the internal weakmap does not have an entry for the object in the second call.
For single argument function such as yours, either:
- Call your function as follows:
const obj = { foo: 'foo', bar: fib };
mfn(obj); // miss
mfn(obj); // hit
- Provide a serializer function.
If a serializer function is provided, and no equals function is provided, the lib will use the serializer function's returned value as a key in the cache (which is a plain object).
So the serializer must return values that can be keys in plain javascript objects - strings or numbers.
The most common is stringifying objects.
obj => JSON.stringify(obj)
... or other string representations that suit you.
If your objects have unique identifier/s and your function only cares about the identity of the objects, I'd return just the identifier/s.
from nano-memoize.
Make sense, thanks for the info!
from nano-memoize.
@igalklebanov thanks for jumping in with a response
from nano-memoize.
@richardscarrott JSON.stringify is slow. Suggest you try to avoid it.
from nano-memoize.
@anywhichway yeah I've found it to be slow. Ultimately I'd like fn({ a, b, c })
and fn(a, b, c)
to be treated in the same way; i.e. I'd like a
, b
and c
to be compared by identity.
const fib = new Fib(); // assume Fib cannot be serialized w/ `JSON.stringify` or `toString`
// This works by default
mfn('foo', 'bar', fib) // miss
mfn('foo', 'bar', fib) // hit
mfn('foo', 'bar', new Fib()) // miss
// But this is obv a miss every time
mfn2({ a: 'foo', b: 'bar', c: fib }) // miss
mfn2({ a: 'foo', b: 'bar', c: fib }) // hit
mfn2({ a: 'foo', b: 'bar', c: new Fib() }) // miss
How does nano-memoize serialize fn(a, b, c)
under the hood?
from nano-memoize.
Related Issues (19)
- IE 11 supported? HOT 5
- Curious dependency: growl HOT 1
- Bug in vrgs function? HOT 2
- Feature idea: Passing dependencies to the memoizer HOT 2
- Cannot build React app using nano-memoize HOT 3
- that setInterval HOT 41
- Question: How to clear the cache? HOT 3
- Issue with `equals` since 3.0.0 with `fast-deep-equal` HOT 3
- callTimeout option notworking HOT 1
- Benchmarking is flawed and nano-memoize is much slower in realistic scenarios HOT 5
- Performance vs nano-memoize
- Missing serializer causes `TypeError: p is not a function` HOT 2
- clearing cache with max age HOT 3
- missing type definitions HOT 2
- what does "equals" do? HOT 1
- Bug on line 40: handling of string argument HOT 5
- TypeScript definitions HOT 5
- Provide es6 modules in package HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nano-memoize.