-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoization.test.js
194 lines (157 loc) · 5.71 KB
/
memoization.test.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const memoization = require("./memoization");
const sinon = require("sinon");
// hint: use https://sinonjs.org/releases/v6.1.5/fake-timers/ for faking timeouts
describe("memoization", () => {
let clock;
beforeEach(() => {
clock = sinon.useFakeTimers(Date.now());
});
afterEach(() => {
clock.uninstall();
});
it("should memoize function result for a definite time", () => {
const testFunction = (year, month, day) => {
return Date.now() + Date(year, month, day);
};
const testMemoizedFunction = memoization.memoize(
testFunction,
(year, month, day) => year + month + day,
1000
);
const firstResult = testMemoizedFunction(1, 11, 26);
clock.tick(100);
const secondResult = testMemoizedFunction(1, 11, 26);
expect(firstResult).toBe(secondResult);
const anotherResult = testMemoizedFunction(2, 12, 26);
expect(anotherResult).not.toBe(firstResult);
clock.tick(500);
const thirdResult = testMemoizedFunction(1, 11, 26);
expect(thirdResult).toBe(secondResult);
clock.tick(1000);
const forthResult = testMemoizedFunction(1, 11, 26);
expect(forthResult).not.toBe(secondResult);
});
it("should use default resolver: memoize(func, timeout)", () => {
const testFunction = (year, month, day) => {
return Date.now() + Date(year, month, day);
};
const testMemoizedFunction = memoization.memoize(testFunction, 1000);
const firstResult = testMemoizedFunction(1, 11, 26);
clock.tick(100);
const secondResult = testMemoizedFunction(1, 11, 26);
expect(firstResult).toBe(secondResult);
const anotherResult = testMemoizedFunction(2, 12, 26);
expect(anotherResult).not.toBe(firstResult);
});
it("should throw error if params are not valid", () => {
const testFunction = (year, month, day) => {
return Date.now() + Date(year, month, day);
};
expect(() => memoization.memoize(testFunction)).toThrow();
expect(() => memoization.memoize(1, (key) => key, 1000)).toThrow();
expect(() => memoization.memoize(testFunction, "hello")).toThrow();
});
it("should handle undefined as cache key", () => {
const testFunction = () => {
return Math.random();
};
const testMemoizedFunction = memoization.memoize(testFunction, 1000);
const firstResult = testMemoizedFunction();
clock.tick(100);
const secondResult = testMemoizedFunction();
expect(firstResult).toBe(secondResult);
clock.tick(1000);
const forthResult = testMemoizedFunction();
expect(forthResult).not.toBe(secondResult);
});
it("should handle null as cache key", () => {
const testFunction = () => {
return Math.random();
};
const testMemoizedFunction = memoization.memoize(
testFunction,
() => null,
1000
);
const firstResult = testMemoizedFunction();
clock.tick(100);
const secondResult = testMemoizedFunction();
expect(firstResult).toBe(secondResult);
clock.tick(1000);
const forthResult = testMemoizedFunction();
expect(forthResult).not.toBe(secondResult);
});
it("should cache async functions result", async () => {
clock.restore();
const asyncRandom = () => {
return new Promise((resolve) =>
setTimeout(() => resolve(Math.random()), 500)
);
};
const testMemoizedFunction = memoization.memoize(
asyncRandom,
() => null,
2000
);
let startTime = Date.now();
const firstResult = await testMemoizedFunction();
let duration = Date.now() - startTime;
expect(duration).toBeGreaterThanOrEqual(500);
startTime = Date.now();
const secondResult = await testMemoizedFunction();
duration = Date.now() - startTime;
expect(duration).toBeLessThan(500);
expect(firstResult).toBe(secondResult);
await new Promise((resolve) => setTimeout(resolve, 2000));
startTime = Date.now();
const thirdResult = await testMemoizedFunction();
duration = Date.now() - startTime;
expect(duration).toBeGreaterThanOrEqual(500);
expect(firstResult).not.toBe(thirdResult);
});
it("should handle object-literal as cache key", async () => {
const testFunction = (year, month, day) => {
return Date.now() + Date(year, month, day);
};
const testMemoizedFunction = memoization.memoize(
testFunction,
(year, month, day) => ({
year,
month,
day,
}),
1000
);
const firstResult = testMemoizedFunction(1, 11, 26);
clock.tick(100);
const secondResult = testMemoizedFunction(1, 11, 26);
expect(firstResult).toBe(secondResult);
const anotherResult = testMemoizedFunction(2, 12, 26);
expect(anotherResult).not.toBe(firstResult);
clock.tick(500);
const thirdResult = testMemoizedFunction(1, 11, 26);
expect(thirdResult).toBe(secondResult);
clock.tick(1000);
const forthResult = testMemoizedFunction(1, 11, 26);
expect(forthResult).not.toBe(secondResult);
});
it("should handle function as cache key", async () => {
const testFunction = (addFunc) => {
return addFunc(Date.now());
};
const testMemoizedFunction = memoization.memoize(testFunction, 1000);
const someAddFunc = (num) => num + 5;
const firstResult = testMemoizedFunction(someAddFunc);
clock.tick(100);
const secondResult = testMemoizedFunction(someAddFunc);
expect(firstResult).toBe(secondResult);
const anotherResult = testMemoizedFunction((num) => someAddFunc(num));
expect(anotherResult).not.toBe(firstResult);
clock.tick(500);
const thirdResult = testMemoizedFunction(someAddFunc);
expect(thirdResult).toBe(secondResult);
clock.tick(1000);
const forthResult = testMemoizedFunction(someAddFunc);
expect(forthResult).not.toBe(secondResult);
});
});