Skip to content

Instantly share code, notes, and snippets.

@lior-amsalem
Created November 12, 2021 06:54
Show Gist options
  • Save lior-amsalem/39c162f014a81c9a95cb66e2de512111 to your computer and use it in GitHub Desktop.
Save lior-amsalem/39c162f014a81c9a95cb66e2de512111 to your computer and use it in GitHub Desktop.
codility cache fibonacci to improve performance
let cache = {};
var yourself = {
fibonacci : function(n) {
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else {
let r = cache[n] = cache[n] ? cache[n] : this.fibonacci(n - 1) +
this.fibonacci(n - 2);
return r;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment