- <!DOCTYPE html>
- <html>
- <head>
- <meta charset='utf-8' />
- <title>Testing Memoization</title>
- </head>
- <body>
- <script type="text/javascript">
- window.onload = function(){
- var fibonacci = function(){
- var memo = [0,1];
- var fib = function(n){
- var result = memo[n];
- if(typeof result != "number"){
- result = fib(n-1) + fib(n-2);
- memo[n] = result;
- }
- return result;
- }
- return fib;
- }();
- console.time('memo');
- console.log(fibonacci(10));
- console.timeEnd('memo');
- }
- </script>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/211120137370.html
来源: http://www.codesnippet.cn/detail/211120137370.html