Last active
August 29, 2015 14:22
-
-
Save joshblack/59fe4597166219ffdc2c to your computer and use it in GitHub Desktop.
Messing around with performance decorators on async methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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