Javascript 记忆化实施
Javascript Memoization Implementation
我一直在和同事讨论这个话题。
我想从你们那里知道,这是否真的是实现记忆的正确方法。
function memoize(result) {
let cache = {};
return function() {
if (cache[result]) {
// returns cached result / no calculation
return cache[result];
}
// calculating...
cache[result] = result;
return cache[result];
};
}
function expensiveCalculation() {
let counter = 0;
for (let i = 0; i < 1000000; i++) {
counter += i;
}
return counter;
}
console.time("FirstCalculation");
const memoizedExpensiveCalculation = memoize(expensiveCalculation());
console.timeEnd("FirstCalculation");
console.time("1_Memoized");
memoizedExpensiveCalculation();
console.timeEnd("1_Memoized");
console.time("2_Memoized");
memoizedExpensiveCalculation();
console.timeEnd("2_Memoized");
console.time("3_Memoized");
memoizedExpensiveCalculation();
console.timeEnd("3_Memoized");
时间日志显示,事实上,第一次花费的时间要长得多(这是我们所期望的),之后花费的时间更少。
不正确
您显示的代码
function memoize(result) {
let cache = {};
return function() {
if (cache[result]) {
// returns cached result / no calculation
return cache[result];
}
// calculating...
cache[result] = result;
return cache[result];
};
}
基本上是
function memoize(result) {
return result
}
在记忆中你缓存的是input/output对并在输入匹配时跳过真正的函数调用。
类似于:
function get(func) {
let cache = {};
return function(key) {
if (key in cache) {
return cache[key];
}
cache[key] = func(key);
return cache[key];
};
}
function expensiveCalculation(param) {
console.log('expensive calculation runs')
let counter = 0;
for (let i = 0; i < 10000; i++) {
counter += i;
}
return param*2;
}
const memo = get(expensiveCalculation)
console.log('start')
console.log('first try');
console.log(memo(10));
console.log('second try');
console.log(memo(10));
我一直在和同事讨论这个话题。 我想从你们那里知道,这是否真的是实现记忆的正确方法。
function memoize(result) {
let cache = {};
return function() {
if (cache[result]) {
// returns cached result / no calculation
return cache[result];
}
// calculating...
cache[result] = result;
return cache[result];
};
}
function expensiveCalculation() {
let counter = 0;
for (let i = 0; i < 1000000; i++) {
counter += i;
}
return counter;
}
console.time("FirstCalculation");
const memoizedExpensiveCalculation = memoize(expensiveCalculation());
console.timeEnd("FirstCalculation");
console.time("1_Memoized");
memoizedExpensiveCalculation();
console.timeEnd("1_Memoized");
console.time("2_Memoized");
memoizedExpensiveCalculation();
console.timeEnd("2_Memoized");
console.time("3_Memoized");
memoizedExpensiveCalculation();
console.timeEnd("3_Memoized");
时间日志显示,事实上,第一次花费的时间要长得多(这是我们所期望的),之后花费的时间更少。
不正确
您显示的代码
function memoize(result) {
let cache = {};
return function() {
if (cache[result]) {
// returns cached result / no calculation
return cache[result];
}
// calculating...
cache[result] = result;
return cache[result];
};
}
基本上是
function memoize(result) {
return result
}
在记忆中你缓存的是input/output对并在输入匹配时跳过真正的函数调用。
类似于:
function get(func) {
let cache = {};
return function(key) {
if (key in cache) {
return cache[key];
}
cache[key] = func(key);
return cache[key];
};
}
function expensiveCalculation(param) {
console.log('expensive calculation runs')
let counter = 0;
for (let i = 0; i < 10000; i++) {
counter += i;
}
return param*2;
}
const memo = get(expensiveCalculation)
console.log('start')
console.log('first try');
console.log(memo(10));
console.log('second try');
console.log(memo(10));