-
-
Notifications
You must be signed in to change notification settings - Fork 64
How to deal with functions taking dict args? #120
Description
Functions that take a dictionary of named arguments is extremely common in JS, for example the entire AWS API does it:
s3.listObjects({Bucket: "some_bucket", Prefix: "some_prefix"})
Memoizing these functions using non-primitive mode results in effectively this comparison after an intended cache hit:
{Bucket: "some_bucket", Prefix: "some_prefix"} === {Bucket: "some_bucket", Prefix: "some_prefix"}
which even though the arguments are exactly the same, returns false because "===" does NOT do deep comparisons on dictionaries and only compares the pointers to the dictionaries, which are different here.
Using primitive mode doesn't work either, since it results in effectively this comparison (based on primitive.js lines 8-18):
Array.prototype.join.call([{Bucket: "some_bucket", Prefix: "some_prefix"}], "|") === Array.prototype.join.call([{Bucket: "some_bucket", Prefix: "some_prefix"}], "|")
which evaluates to
"[object Object]" === "[object Object]"
which although returns true, it is not what we want, because it will evaluate any two dictionaries as equal even if their contents are different.
What I really want is a deep compare, and I think that can be effectively accomplished for serializable arguments by using JSON.stringify instead of Array.prototype.join, which will serialize the dictionaries to strings instead of converting them to the literal string "[object Object]".
Let me know if you agree and I'll be happy to create a pull request, or let me know if you already have plans to resolve this use case another way.