chuanxshi / javascript-patterns

JavaScript Design Patterns

Home Page:http://shichuan.github.io/javascript-patterns

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

memoization using multiple argument as JSON-key

Bartvds opened this issue · comments

I was looking at the memoization patterns and specifically 'preferred method 2' (multiple arguments using JSON stringify) and noticed a possible issue:

var cachekey = JSON.stringify(Array.prototype.slice.call(arguments)),

Maybe it should mention that this will only work correctly for primitive (and Array) arguments, since the order in which properties of Objects are enumerated is undetermined and can even change between enumerations. (a JSON encoder that sorts the keys won't have this issue).

Take for example this object:

{a:1, b:2}

Could on execution result in theseJSONs and so miss the cache.
"{'a':1, 'b':2}"
"{'b':2, 'a':1}"

thanx for the comment, note added :)