Skip to content

Instantly share code, notes, and snippets.

@lahin31
Last active January 14, 2019 17:24
Show Gist options
  • Save lahin31/5c8844afb3b5b2b7e8ed6d8de2617fd1 to your computer and use it in GitHub Desktop.
Save lahin31/5c8844afb3b5b2b7e8ed6d8de2617fd1 to your computer and use it in GitHub Desktop.
/*
Simple Memoization example used Closure
Made by: Muhammad Lahin
*/
const myObj = {memo: {}}
const memo = fn => {
return (first_name, last_name) => {
let slice = Array.prototype.slice;
let key = JSON.stringify(slice.call(arguments));
if(!myObj.memo[key]) {
let result = {}
result.first_name = fn(first_name);
result.last_name = fn(last_name);
myObj.memo[key] = result;
}
return myObj.memo[key];
}
}
function firstLetterUpperCase(name) {
return name.charAt(0).toUpperCase() + name.slice(1);
}
let _firstLetterUpperCase = memo(firstLetterUpperCase);
console.time()
console.log(_firstLetterUpperCase("lahin", "hussain"))
console.timeEnd()
console.time()
console.log(_firstLetterUpperCase("lahin", "hussain"))
console.timeEnd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment