Created
November 2, 2015 13:00
-
-
Save dhoko/828797682e2bf8822c18 to your computer and use it in GitHub Desktop.
Détection du type de connexion
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
| /** | |
| * 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