-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.test.ts
53 lines (50 loc) · 1.77 KB
/
performance.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { initMemofy, memoize } from "../lib";
describe("Performance Tests", () => {
initMemofy();
test("should execute heavy method with and without caching", async () => {
const heavyComputation = jest.fn(
async (inputNumber: number): Promise<number> => {
let sum = 0;
function isPrime(num: number): boolean {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
let i = 5;
while (i * i <= num) {
if (num % i === 0 || num % (i + 2) === 0) return false;
i += 6;
}
return true;
}
for (let i = 2; i <= inputNumber; i++) {
while (inputNumber % i === 0) {
if (isPrime(i)) {
sum += i;
}
inputNumber /= i;
}
}
return sum;
}
);
// Before caching
const _heavyComputation = memoize(heavyComputation);
const startTime = performance.now();
const result = await _heavyComputation(91012323);
const endTime = performance.now();
const timeDiff = endTime - startTime;
console.log("Before caching", timeDiff, " ms");
expect(result).toEqual(30337444);
expect(timeDiff).toBeGreaterThanOrEqual(45);
expect(heavyComputation).toHaveBeenCalledTimes(1);
// After caching
const newStartTime = performance.now();
const newResult = await _heavyComputation(91012323);
const newEndTime = performance.now();
const newTimeDiff = newEndTime - newStartTime;
console.log("After caching", newTimeDiff, " ms");
expect(newResult).toEqual(30337444);
expect(newTimeDiff).toBeLessThanOrEqual(1);
expect(heavyComputation).toHaveBeenCalledTimes(1); // Because value returned cache
});
});