Permalink
Browse files

Add memoization

  • Loading branch information...
koenbok committed Sep 26, 2014
1 parent bd01fd8 commit dc36471cc7e8c8e382d4299dfca0f539f591be92
Showing with 12 additions and 0 deletions.
  1. +12 −0 framer/Utils.coffee
@@ -120,6 +120,18 @@ Utils.throttle = (delay, fn) ->
setTimeout (-> timer = false), delay unless delay is -1
fn arguments...
# Taken from http://addyosmani.com/blog/faster-javascript-memoization/
Utils.memoize = (fn) -> ->
args = Array::slice.call(arguments)
hash = ""
i = args.length
currentArg = null
while i--
currentArg = args[i]
hash += (if (currentArg is Object(currentArg)) then JSON.stringify(currentArg) else currentArg)
fn.memoize or (fn.memoize = {})
(if (hash of fn.memoize) then fn.memoize[hash] else fn.memoize[hash] = fn.apply(this, args))
######################################################
# HANDY FUNCTIONS

0 comments on commit dc36471

Please sign in to comment.