Skip to content

Commit

Permalink
do not call Date.now twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Razon committed Sep 27, 2023
1 parent 157ec48 commit 23bb24c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tunnel-server/src/memoize.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export const memoizeForDuration = <T>(f: () => T, milliseconds: number) => {
let cache: { value: T; expiry: number } | undefined
return () => {
if (!cache || cache.expiry <= Date.now()) {
cache = { value: f(), expiry: Date.now() + milliseconds }
const now = Date.now()
if (!cache || cache.expiry <= now) {
cache = { value: f(), expiry: now + milliseconds }
}
return cache.value
}
Expand Down

0 comments on commit 23bb24c

Please sign in to comment.