Skip to content

Instantly share code, notes, and snippets.

@aramk
Created November 7, 2014 11:50
Show Gist options
  • Save aramk/21a6d2da746ad4765dbe to your computer and use it in GitHub Desktop.
Save aramk/21a6d2da746ad4765dbe to your computer and use it in GitHub Desktop.
Support for object arguments in _.memoize()
# Adds support for using object arguments with _.memoize().
nextMemoizeObjKey = 1
memoizeObjectArg = (func) ->
_.memoize func, (obj) ->
key = obj._memoizeObjKey
unless key?
key = obj._memoizeObjKey = nextMemoizeObjKey++
key
# Example:
fooFunc = memoizeObjectArg (obj) -> Object.keys(obj)
arg = {foo: 'bar'}
fooFunc(arg) # => ['foo']
# The same memoized value is returned regardless of changes.
arg.foo = 'bar2'
fooFunc(arg) # => ['foo']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment