Comments (14)
@ecaron this is expected, in second case at all times function receives different object, therefore it's treated as different argument. Values by default are compared with ===
(and more precisely by this logic)
If you want to have different objects but with same structure treated as same, then you need to provide custom normalizer function that will assure that.
e.g. your example will produce cache hits, for below configuration:
var memoized = memoize(cacheTest, { async: true, normalizer: JSON.stringify });
EDIT (made after below comments):
As normalizer receives arguments object, it actually should be:
var memoized = memoize(cacheTest, { async: true, normalizer: function (args) {
return JSON.stringify(args[0]);
} });
from memoizee.
@vekexasia can you post minimal test case that in your eyes doesn't work as expected?
from memoizee.
@vekexasia as I don't have any working test case that would replicate a problem, and I'm not aware of any issues in memoizee. I'm closing this one
from memoizee.
@medikoo, I'm guessing the issue that @vekexasia is experiencing is this has cache hits:
var memoize = require('memoizee');
var cacheTest = function(a, cb){
console.log("Cache missed");
return cb(null, a);
};
var memoized = memoize(cacheTest, { async: true });
var test = {a: 1};
memoized(test, function(err, res){
console.log(res);
memoized(test, function(err, res){
console.log(res);
memoized(test, function(err, res){
console.log(res);
process.exit();
});
});
});
But this one only has cache misses
var memoize = require('memoizee');
var cacheTest = function(a, cb){
console.log("Cache missed");
return cb(null, a);
};
var memoized = memoize(cacheTest, { async: true });
memoized({a: 1}, function(err, res){
console.log(res);
memoized({a: 1}, function(err, res){
console.log(res);
memoized({a: 1}, function(err, res){
console.log(res);
process.exit();
});
});
});
from memoizee.
(thumbsup)
from memoizee.
@medikoo But the normalizer applies to every argument in the function, rather than just limiting to the ones expected by the length parameter. So, for example, if you're using with and wanting to pass along a req-esque parameter outside the cache-specific parameters, you'll get a circular structure error.
/me works on providing an example
from memoizee.
Here's the code showing that the normalizer is getting values that I was expecting the length parameter to overlook:
var memoize = require('memoizee');
var cacheTest = function(a, req, cb){
return cb(null, a);
};
var whatIGot = function(a){
//I expect the normalizer to be only working on the values that the length parameter implies
console.log(a);
return JSON.stringify(a);
}
var memoized = memoize(cacheTest, { async: true, length: 1, normalizer: whatIGot });
memoized({a: 1}, {should: "not stringify me"}, function(err, res){
console.log(res);
});
from memoizee.
@ecaron sorry I was not precise, what normalizer
receives is arguments object of function call, and it's role is to return unique id on which bases uniqueness of arguments will be assumed.
So if you're interested only in first argument, same you're normalizer should generate id only against first one. So what I wrote, should actually be:
var memoized = memoize(cacheTest, { async: true, normalizer: function (args) {
return JSON.stringify(args[0]);
} });
from memoizee.
@medikoo And that makes sense, but as a consumer of the library and given the explanation within https://github.com/medikoo/memoize#arguments-length, I'd expect the uniqueness of the arguments to correlate to the length desired to impact the cache strategy.
Would you consider a PR that adjusts the normalizer to rely on the length
parameter if its explicitly defined?
from memoizee.
@medikoo And that makes sense, but as a consumer of the library and given the explanation within https://github.com/medikoo/memoize#arguments-length, I'd expect the uniqueness of the arguments to correlate to the length desired to impact the cache strategy.
It's normally assured, however normalizer takes original arguments object, and arguments It's the object that we should not alter in any way.
The only fix for that would be to change normalizer strategy, so instead of arguments object it will receive arguments same way as input function, so as e.g. normalizer(arg1, arg2, ...args)
and in that case we could limit them against defined limit.
However this would definitely downgrade performance, firstly we would need to call normalizer with apply
, and logic that would filter them against limit again will put perf down further.
So I think I would prefer to stay with what it is. It's very direct to just pass arguments over, and additionally I think it's not that big issue when you know it's an actual arguments object. Above we forgot about it, and that was the main issue.
from memoizee.
How do you retrieve data once data is cached?
from memoizee.
How do you retrieve data once data is cached?
@nguyeti In normal mode input arguments are stored in an array, which is an index mapping to result array. So knowing index of input argument you get to know index of result within result array.
from memoizee.
Thank you very much.
I noticed that the input arguments are used to identify the data in the delete method for example. But I can't find the ext Get method you and epayet exposed, how can I use it to access the result array?
from memoizee.
@nguyeti there's no direct access to cache provided. What use case you have for that?
from memoizee.
Related Issues (20)
- Cannot find module 'event-emitter' HOT 3
- Dependency on "D" renders this useless HOT 4
- Doesn't work with snowpack. HOT 3
- Is not caching non-promise returning calls the intended behavior? HOT 1
- Promise rejections not cleared immediately from cache HOT 2
- Callback for cache access HOT 5
- setter method HOT 1
- How to deal with functions taking dict args? HOT 6
- Rollup fails to bundle memoizee correctly HOT 6
- feat: let default nomalizer be more general HOT 1
- replace `es5-ext` with `core-js@^3` HOT 2
- Cannot import memoizee HOT 3
- Memoizing a function without arguments HOT 1
- Allow `maxAgeSinceAccess` in addition to `maxAge` HOT 11
- Why is the memoizee so slow? HOT 2
- Is there a way to dynamically configure the size? HOT 7
- Feature Request: Wouldn't it be good if we could use a Redis backend? HOT 2
- max option does not work with promise option HOT 1
- Option to return prior cached value on update failure HOT 5
- Package is showing a vulnerability HOT 7
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 memoizee.