Skip to content

Instantly share code, notes, and snippets.

@iinsta
Last active January 28, 2018 18:22
Show Gist options
  • Save iinsta/fa3b6fc868e7a76b3b8ab40b0b36f365 to your computer and use it in GitHub Desktop.
Save iinsta/fa3b6fc868e7a76b3b8ab40b0b36f365 to your computer and use it in GitHub Desktop.
speed test js
function mitt(all) {
all = all || Object.create(null)
return {
on(type, cb){
(all[type] || (all[type] = [])).push(cb);
},
off(type, cb){
if (all[type]) {
all[type].splice(all[type].indexOf(cb) >>> 0, 1);
}
},
emit(type, evt){
(all[type] || []).map(cb => { cb(evt); });
(all['*'] || []).map(cb => { cb(type, evt); });
}
}
}
/*
* usage:
* const speed = speedTest(function(result){
* console.log(result)
* })
* speed({url: 'domain.com', method: 'get'})
*
*/
function speedTest(cb){
const o = mitt();
o.on('finish', cb);
return function(options) {
const client = new XMLHttpRequest();
let startTime = null
client.onreadystatechange = function() {
switch(client.readyState) {
case 1: /* Opened */
startTime = Date.now();
break;
case 4:
if (client.status == 200) {
console.log('finish')
}
break;
}
}
client.open(options.method, options.url);
client.send(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment