Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | /**
* A class that helps with determining network speed.
*/
export class networkSpeed {
static mbsp: number;
/**
* This method loads an image to try to determine the network speed.
* There are several issues with it. Safari doesn't report times very precisely
* so it can be wildely off. Generally, small loads result in horrible
* accuracy.
*
* This method is a still being tested and can change.
* @param testImageSource
*/
static getMbspFromImage(testImageSource: string): Promise<number> {
return new Promise((resolve) => {
// First load the fallback image and approximate the
// network speed.
fetch(testImageSource)
.then((response) => {
return response.blob();
})
.then((blob) => {
let speed = 0;
// Get the performance.
let entries = performance.getEntriesByType('resource');
entries = entries.filter((entry) => {
return entry.name === testImageSource;
});
const matchedEntry = entries[0];
// Use blob.size since Safari doesn't contain byte size info
// in entry.
const fileSizeMb = blob.size / 1000000;
const responseTime = matchedEntry.duration / 1000;
console.log(matchedEntry);
speed = fileSizeMb / responseTime;
// Account for being off by a factor of 10 for some reason.
speed *= 100;
resolve(speed);
});
});
}
/**
* Measures the time the root document (HTML) took to be sent from server.
* This is fairly consistent across each reload of a given page (assuming)
* your server is stable.
*
* Return the MBSP or null. Null represents cases in which a value can't
* be determined.
*
* LIMITED SUPPORT:
* Note that on safari, due to feature limitations, this will return null.
*
* @see https://stackoverflow.com/questions/16808486/explanation-of-window-performance-javascript
* @see https://w3c.github.io/navigation-timing/
* @see https://stackoverflow.com/questions/16808486/explanation-of-window-performance-javascript
*/
static getMbsp(): number | null {
// Guard against old browsers.
if (!performance || !performance.getEntriesByType) {
return null;
}
// Run just once.
if (networkSpeed.mbsp) {
return networkSpeed.mbsp;
}
const responseTime =
performance.timing.domContentLoadedEventStart -
performance.timing.requestStart;
const duration = responseTime;
const durationSeconds = duration / 1000;
const amountOfDataMb = networkSpeed.getDataTransfer() / 1000000;
// If the amount of data coudn't be comprehended. This happens
// on safari where
if (!amountOfDataMb) {
return null;
}
let approxSpeed = amountOfDataMb / durationSeconds;
// Account for being off by a factor of 10 for some reason.
approxSpeed *= 10;
// Save results.
networkSpeed.mbsp = approxSpeed;
return approxSpeed;
}
/**
* Gets the total amount of transferred resources on the page.
* LIMITED SUPPORT:
* Note that this method does not work on Safari since safari doesn't return
* data size info.
*/
static getDataTransfer() {
const entries = performance.getEntriesByType('resource') as Array<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Record<string, any>
>;
let size = 0;
entries.forEach((entry) => {
size += entry['transferSize'];
});
return size;
}
}
|