Created
May 11, 2015 09:48
-
-
Save benben77/2aadde58380e5280db27 to your computer and use it in GitHub Desktop.
JS Function Utils: debounce, throttle, curry, memorize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.FunctionHelpers = { | |
debounce: function(fn, threshold, execASAP) { | |
var timeout = null; | |
threshold = threshold || 100; | |
return function() { | |
var self = this; | |
var args = arguments; | |
var delayed = function() { | |
if (!execASAP) { | |
fn.apply(self, args); | |
} | |
timeout = null; | |
}; | |
if (timeout) { | |
clearTimeout(timeout); | |
} else if (execASAP) { | |
fn.apply(self, args); | |
} | |
timeout = setTimeout(delayed, threshold); | |
}; | |
}, | |
throttle: function(fn, threshold, alt) { | |
var last = new Date(1970); | |
threshold = threshold || 100; | |
return function() { | |
var now = Date.now(); | |
if (now - last < threshold) { | |
if (alt) { | |
alt.apply(this, arguments); | |
} | |
return; | |
} | |
last = now; | |
fn.apply(this, arguments); | |
}; | |
}, | |
remember: function(fn, keyfn) { | |
var results = {}; | |
return function() { | |
var key = keyfn.apply(undefined, arguments); | |
if (!results[key]) { | |
results[key] = fn.apply(undefined, arguments); | |
} | |
return results[key]; | |
} | |
}, | |
curry: function(fn) { | |
var args = [].slice.call(arguments, 1), | |
placeholder = arguments.callee.placeholder, | |
placeholders = []; | |
for (var i = 0; i < args.length; i++) { | |
if (args[i] === placeholder) { | |
placeholders.push(i); | |
} | |
} | |
if (placeholders.length > 0) { | |
return function() { | |
var inputs = [].slice.call(arguments), | |
origin = args.slice(); | |
for (var i = 0; i < placeholders.length; i++) { | |
origin[placeholders[i]] = inputs.shift(); | |
} | |
fn.apply(undefined, [].concat.apply(origin, inputs)); | |
}; | |
} else { | |
return function() { | |
fn.apply(undefined, [].concat.apply(args, arguments)); | |
}; | |
} | |
} | |
} | |
window.FunctionHelpers.curry.placeholder = {}; | |
Examples: | |
var logA = FunctionHelpers.debounce(function () { | |
console.log('A'); | |
}, 1000); | |
logA(); | |
var logB = FunctionHelpers.throttle(function () { | |
console.log('B'); | |
}, 1000); | |
logB(); | |
var logC = FunctionHelpers.curry(function () { | |
console.log([].join.apply(arguments)); | |
}, 'a', 'b'); | |
logC('c'); | |
var logC2 = FunctionHelpers.curry(function () { | |
console.log([].join.apply(arguments)); | |
}, window.FunctionHelpers.curry.placeholder, 'b2', 'c2'); | |
logC2('a2'); | |
var Fib = FunctionHelpers.remember(function (n) { | |
console.log(n); | |
switch (n) { | |
case 1: | |
return 1; | |
case 2: | |
return 2; | |
default: | |
return Fib(n-1) + Fib(n-2); | |
} | |
}, function (n) { | |
return n; | |
}); | |
console.log(Fib(10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment