Skip to content

Instantly share code, notes, and snippets.

@joshblack
Last active August 29, 2015 14:22
Show Gist options
  • Save joshblack/59fe4597166219ffdc2c to your computer and use it in GitHub Desktop.
Save joshblack/59fe4597166219ffdc2c to your computer and use it in GitHub Desktop.
Messing around with performance decorators on async methods
function measure(target, name, descriptor) {
const fn = descriptor.value;
descriptor.value = async function () {
const mark = `mark_${name}`;
performance.mark(mark);
const result = await fn();
performance.measure(mark);
const perf = performance.getEntriesByName(mark);
// send perf measurements to worker
clear(mark);
return result;
}
}
function clear(mark) {
performance.clearMarks(mark);
performance.clearMeasures(mark);
}
// Usage
class ClientAPI {
@measure
async retrieve() {
const result = await foo();
return result;
}
}
function foo() {
return new Promise(
(res, rej) => setTimeout(() => res('success'), 3000));
}
(async function () {
const a = new AsyncFoo();
const data = await a.retrieve();
console.log(data); // => 'success'
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment