Created
December 3, 2019 13:55
-
-
Save tolotrasmile/6047a650bc7cdebea790dd0f963fec23 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
function memoize(fn) { | |
let cache = {} | |
return function (...args) { | |
if (cache[args]) { | |
return cache[args] | |
} | |
const res = fn.apply(this, args) | |
cache[args] = res | |
return res | |
} | |
} | |
const fib = memoize((n) => (n < 2) ? n : fib(n - 1) + fib(n - 2)) | |
function assertion(condition, message = 'Assertion failed') { | |
if (!condition) { | |
throw message | |
} | |
} | |
console.time('fibonacci time'); | |
assertion(fib(0) + fib(1) === fib(2)) | |
console.timeEnd('fibonacci time'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment