elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.83k stars 98 forks source link

Timing for each request #6

Closed motss closed 7 years ago

motss commented 7 years ago

Something like time: true in [request][https://www.npmjs.com/package/request].

elbywan commented 7 years ago

For browsers the Resource Timing API does exactly what you need and can be used quite easily !

wretch("...").get().res(res => {
    const entries = performance.getEntriesByName(res.url)
    console.log(entries[entries.length - 1])
})

For node.js it seems quite impossible since wretch depends on the polyfill used (which can be anything, with different implementations).

elbywan commented 7 years ago

Okay I implemented something :

// Usage :
wretch("...").get().perfs(performanceTimings => {
    /* ... */
})
.json()
/*...*/

Example of a performance object returned by chrome :

{
    "name": "...",
    "entryType": "resource",
    "startTime": 22405.84,
    "duration": 3.9650000000037835,
    "initiatorType": "",
    "nextHopProtocol": "http/1.1",
    "workerStart": 0,
    "redirectStart": 0,
    "redirectEnd": 0,
    "fetchStart": 22405.84,
    "domainLookupStart": 22405.84,
    "domainLookupEnd": 22405.84,
    "connectStart": 22405.84,
    "connectEnd": 22405.84,
    "secureConnectionStart": 0,
    "requestStart": 22407.97,
    "responseStart": 22409.275,
    "responseEnd": 22409.805000000004,
    "transferSize": 2618,
    "encodedBodySize": 2322,
    "decodedBodySize": 2322,
    "serverTiming": []
}

The problem is that it's relying on highly experimental features on browsers and node.js.

I put the code on a separate branch for now.

elbywan commented 7 years ago

@motss

I improved the code and made it partially compatible with node 🎉 . It's published in the new release. Check out the docs.

motss commented 7 years ago

Great job. I'm asking timings support for Node.js. definitely check it out later.