Skip to content

Commit

Permalink
📝 Perfs() documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Oct 6, 2017
1 parent 3a14499 commit 6c90e15
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Expand Up @@ -599,6 +599,48 @@ Text handler.
wretch("...").get().text(txt => console.log(txt))
```
## Performance API (experimental)
#### perfs(cb: (timings: PerformanceTiming) => void)
*Highly experimental*
Takes advantage of the Performance API ([browsers](https://developer.mozilla.org/en-US/docs/Web/API/Performance_API) & [node.js](https://nodejs.org/api/perf_hooks.html)) to expose timings related to the request.
Browser timings are very accurate, node.js only contains raw measures.
```js
// Use perfs() before the response types (text, json, ...)
wretch("...")
.get()
.perfs(timings => {
/* Will be called when the timings are ready. */
console.log(timings.startTime)
})
.res()
/* ... */
```
For node.js, there is a little extra work to do :
```js
// Node.js 8.5+ only
const { performance, PerformanceObserver } = require("perf_hooks")

const fetchPolyfill =
wretch().polyfills({
fetch: function(url, opts) {
performance.mark(url + " - begin")
return fetch(url, opts).then(_ => {
performance.mark(url + " - end")
performance.measure(_.url, url + " - begin", url + " - end")
})
},
/* ... */
PerformanceObserver: PerformanceObserver
})
```
# License
MIT

0 comments on commit 6c90e15

Please sign in to comment.