Perf: use Bun.peek() to save a few ticks

This commit is contained in:
SukkaW
2024-01-28 22:29:14 +08:00
parent c95e96fc61
commit e626a6b5d2
13 changed files with 108 additions and 47 deletions

View File

@@ -158,9 +158,21 @@ export class Cache {
let value: T;
if (cached == null) {
console.log(picocolors.yellow('[cache] miss'), picocolors.gray(key), picocolors.gray(`ttl: ${TTL.humanReadable(ttl)}`));
value = await fn();
const serializer = 'serializer' in opt ? opt.serializer : identity;
const promise = fn();
const peeked = Bun.peek(promise);
if (peeked === promise) {
return promise.then((value) => {
const serializer = 'serializer' in opt ? opt.serializer : identity;
this.set(key, serializer(value), ttl);
return value;
});
}
value = peeked as T;
this.set(key, serializer(value), ttl);
} else {
console.log(picocolors.green('[cache] hit'), picocolors.gray(key));
@@ -168,6 +180,7 @@ export class Cache {
const deserializer = 'deserializer' in opt ? opt.deserializer : identity;
value = deserializer(cached);
}
return value;
}