Skip to content

Instantly share code, notes, and snippets.

@dhoko
Created November 2, 2015 13:00
Show Gist options
  • Select an option

  • Save dhoko/828797682e2bf8822c18 to your computer and use it in GitHub Desktop.

Select an option

Save dhoko/828797682e2bf8822c18 to your computer and use it in GitHub Desktop.
Détection du type de connexion
/**
* Detect the type of network
* @link http://caniuse.com/#feat=resource-timing
*/
function getNetwork() {
// Default case if the network detection api is not available
if (!window.performance || !window.performance.getEntriesByName) {
return '4G';
}
const js = performance.getEntriesByName(location.origin + '/js/vendor.min.js')[0] || {duration: 290};
const DELTA = js.duration * 1.2;
// For a file 80ko Gzipped
const map = {
dsl : 40,
'4G' : 290,
'3G+' : 740,
'3G' : 1550,
'2G' : 4550
};
return Object
.keys(map)
.reduce((prev, curr) => {
return (Math.abs(map[curr] - DELTA) < Math.abs(map[prev] - DELTA) ? curr : prev);
}, 'dsl');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment