<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>每天一个JavaScript实例-使用缓存计算(memoization)来提高应用程序性能</title>
<script>
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;
}
console.log("memo"+memo);
return result; }; return fib; }(); var fib =function(n){
return n<2? n:fib(n-1)+fib(n-2);
}; console.time("non-memo");
for(var i = 0; i<=10;i++){
console.log(i+ " " + fib(i));
}
console.timeEnd("non-memo");
console.time("memo");
for(var i = 0; i<=10;i++){
console.log(i+ " " + fibonacci(i) );
}
console.timeEnd("memo");
}
</script>
</head> <body> </body>
</html>
版权声明:本文博客原创文章,博客,未经同意,不得转载。