Why is the memoizee so slow?
rudimadima opened this issue · comments
rudimadima commented
Simple example:
const memoize = require("memoizee");
function triangleArea(a, b, c) {
if ((a + b <= c) || (b + c <= a) || (a + c <= b)) {
throw new Error('Wrong triangle');
}
const p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
};
const triangleAreaMemo = memoize(triangleArea);
let result1;
console.time('withoutCache');
for (let i = 0; i < 1000000; i++) {
result1 = triangleArea(500, 500, 900);
}
console.timeEnd('withoutCache'); // 3.595ms
let result2;
console.time('withCache');
for (let i = 0; i < 1000000; i++) {
result2 = triangleAreaMemo(500, 500, 900);
}
console.timeEnd('withCache'); // 74.507ms or 173.055ms with { primitive: true }
Why is the memoized function 20 times slower?
Mariusz Nowak commented
@rudimadima thanks for opening. I've received a very similar question in the past, please see my answer, I think it explains well: #27 (comment)
rudimadima commented
@medikoo, thank you for the answer!