Skip to content

VYNYL/isfastnet

 
 

Repository files navigation

Built on top of this Stack Overflow answer.

Version 2

Breaking change

A default export has been added now to ensure that the package can be integrated smoothly when using it with ES6 imports. Example 3 has been added to indicate the same

Examples

In the browser

<script type="module">
    import isFastNet from 'https://cdn.jsdelivr.net/npm/[email protected]'
    isFastNet((value) => {
    let speed = value ? 'fast':'slow';
    console.log('Internet is ' + speed);
})
</script>

As an ES6 import

<script>
import isFastNet from 'isFastNet'
isFastNet((data) => {
    /**
    * data = {
    *   "isFast": Boolean,
    *   "averageLatency": Number,
    *   "threshold": Number,
    *   "latencyReadings": [Number]
    * } 
    */
    console.log('Received latency data: ' + JSON.stringify(data));
}, { verbose: true })
</script>

With verbose option

<script type="module">
    import isFastNet from 'https://cdn.jsdelivr.net/npm/[email protected]'
    isFastNet((data) => {
        /**
        * data = {
        *   "isFast": Boolean,
        *   "averageLatency": Number,
        *   "threshold": Number,
        *   "latencyReadings": [Number]
        * } 
        */
        console.log('Received latency data: ' + JSON.stringify(data));
    }, { verbose: true })
</script>

Using with nuxt.js

Do a normal ES6 import

import isFastNet from 'isFastNet'

Add isfastnet to the transpile option in nuxt.config.js

...
build: {
    transpile: ['isfastnet']
}
...

Usage

isFastNet((value) => {
    if(value){
        // Internet is fast
    }
    else{
        // Internet is slow
    }
}, { 
timesToTest: 5, // optional, number of times to load the image default is 5
threshold: 200, // optional, threshold in ms after which internet speed is considered slow
image:  "http://www.google.com/images/phd/px.gif", //  optional, url of the tiny image to load, keep this on a CDN
allowEarlyExit: true, // optional, if the first request takes greater than threshold*3 ms then the function exits with false
verbose: false  // optional, if set, it returns an object with all calculated latency data. Overrides allowEarlyExit option (See Example "With verbose option" for usage)
})

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 58.8%
  • HTML 41.2%