diff --git a/lib/async.js b/lib/async.js index 3b212cca8..31e7620fb 100644 --- a/lib/async.js +++ b/lib/async.js @@ -1085,16 +1085,17 @@ async.memoize = function (fn, hasher) { var memo = {}; var queues = {}; + var has = Object.prototype.hasOwnProperty; hasher = hasher || identity; var memoized = _restParam(function memoized(args) { var callback = args.pop(); var key = hasher.apply(null, args); - if (key in memo) { + if (has.call(memo, key)) { async.setImmediate(function () { callback.apply(null, memo[key]); }); } - else if (key in queues) { + else if (has.call(queues, key)) { queues[key].push(callback); } else { diff --git a/test/test-async.js b/test/test-async.js index 05c022827..e6fe8effd 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -4309,6 +4309,45 @@ exports['memoize'] = { test.equal(val, "bar"); test.done(); }); +}, + + 'avoid constructor key return undefined': function (test) { + test.expect(1); + var fn = async.memoize(function(name, callback) { + setTimeout(function(){ + callback(null, name); + }, 100); + }); + fn('constructor', function(error, results) { + test.equal(results, 'constructor'); + test.done(); + }); +}, + + 'avoid __proto__ key return undefined': function (test) { + test.expect(1); + var fn = async.memoize(function(name, callback) { + setTimeout(function(){ + callback(null, name); + }, 100); + }); + fn('__proto__', function(error, results) { + test.equal(results, '__proto__'); + test.done(); + }); +}, + + 'allow hasOwnProperty as key': function (test) { + test.expect(1); + var fn = async.memoize(function(name, callback) { + setTimeout(function(){ + callback(null, name); + }, 100); + }); + fn('hasOwnProperty', function(error, results) { + test.equal(results, 'hasOwnProperty'); + test.done(); + }); } };