Last active
January 14, 2019 17:24
-
-
Save lahin31/5c8844afb3b5b2b7e8ed6d8de2617fd1 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
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