caolan/async — Issue #96: .memoize() does not work with object arguments
Author: debonet
Created: 2012-02-17 15:10:55+00:00
State: open

.memoize() default has does not distinguish between different input objects. Here's a sample program:

var Async = require("async");

var f = Async.memoize(
function(o,fCallback){
console.log("In f() with", o);
fCallback(null,o.a);
}
);

f({a:1},function(err,x){console.log(x);});
f({a:2},function(err,x){console.log(x);});

Note the erroneous output:

In f() with { a: 1 }
1
1

Where the expected output should be:

In f() with { a: 1 }
1
In f() with { a: 2 }
2

A simple fix is to make the default hash function JSON.stringify, instead of function(x){return x;}

Posting here, because pull requests seem to be falling behind, and it might be easier on caolan to do the patch directly. Let me know if you'd prefer a pull request.