-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
29 lines (26 loc) · 736 Bytes
/
cache.js
File metadata and controls
29 lines (26 loc) · 736 Bytes
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
const { performance } = require('perf_hooks');
(async function () {
let cache = new Map()
async function getUser(id) {
if (cache.has(id)) {
return cache.get(id)
}
const url = 'https://jsonplaceholder.typicode.com/users/' + id
const user = await fetch(url).then(response => response.json())
cache.set(id, user)
return cache.get(id)
}
const start = performance.now();
await getUser(1)
await getUser(1)
await getUser(1)
await getUser(1)
await getUser(1)
await getUser(1)
await getUser(1)
await getUser(1)
await getUser(1)
const end = performance.now();
console.log(`${end - start}ms.`);
console.log(cache);
})()