Last active
March 18, 2020 10:25
-
-
Save droganaida/110df5176ee0e6c0ff922148b5dc3147 to your computer and use it in GitHub Desktop.
Asynchronous vs synchronous vs synchronous asynchronous loop with delay or without
This file contains 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
const request = require("request"); | |
const Promise = require('bluebird'); | |
const getUrl = Promise.promisifyAll(request); | |
let requestsSent = 0; | |
let requestsSuccess = 0; | |
//======================= jsonGet Function =======================// | |
async function jsonGet (day=1, month=0, delay=0){ | |
const options = { | |
host: 'numbersapi.p.rapidapi.com', | |
port: 443, | |
path: `/${month}/${day}/date?fragment=true&json=true`, | |
headers: { | |
"X-RapidAPI-Host": "numbersapi.p.rapidapi.com", | |
"X-RapidAPI-Key": "YOUR_KEY" | |
} | |
}; | |
await Promise.delay(delay); | |
return await getUrl.getAsync(`https://${options.host}${options.path}`, {headers: options.headers}); | |
} | |
function getDaysArray(startDate, endDate) { | |
let dateArray = []; | |
for( let dt=startDate; dt<=endDate; dt.setDate(dt.getDate()+1)){ | |
const date = new Date(dt); | |
dateArray.push({day: date.getDate(), month: date.getMonth()}); | |
} | |
return dateArray; | |
} | |
async function getAllPrograms() { | |
const YEARS = 1; | |
const startDate = new Date(new Date().setFullYear(new Date().getFullYear() - YEARS)); | |
const endDate = new Date(); | |
const dates = getDaysArray(startDate, endDate); | |
requestsSent = dates.length; | |
let counter = 0; | |
const timeOffset = 1000; | |
// ============ sync LOOP (with delay) ============== // | |
// time 183.902s - 1y | |
return Promise.each(dates, date => jsonGet(date.day, date.month, timeOffset) | |
.then(responseData =>{ | |
counter++; | |
try { | |
const data = JSON.parse(responseData.body); | |
if (data && data.number && data.year && data.text) { | |
requestsSuccess++; | |
console.log(`${counter} :: Year: ${data.year}, fact №: ${data.number}, - ${data.text}`) | |
} else { | |
console.log(`${counter} :: REQUEST FAIL`) | |
} | |
} catch (err) { | |
console.log(`${counter} :: REQUEST FAIL`); | |
} | |
})); | |
// ============ async LOOP (with delay) ============== // | |
// time 2.155s - 1y | |
// time 23.37s - 10y | |
// const step = 100; | |
// | |
// return Promise.map(dates, date => jsonGet(date.day, date.month, timeOffset) | |
// .then(responseData =>{ | |
// counter++; | |
// try { | |
// const data = JSON.parse(responseData.body); | |
// | |
// if (data && data.number && data.year && data.text) { | |
// requestsSuccess++; | |
// console.log(`${counter} :: Year: ${data.year}, fact №: ${data.number}, - ${data.text}`) | |
// } else { | |
// console.log(`${counter} :: REQUEST FAIL`) | |
// } | |
// | |
// } catch (err) { | |
// console.log(`${counter} :: REQUEST FAIL`); | |
// } | |
// }), | |
// {concurrency: step} | |
// ); | |
} | |
const START_TIME = new Date(); | |
process.stdout.write('Test start'); | |
(async() => { | |
try { | |
await getAllPrograms(); | |
console.log(`========= Requests: ${requestsSent}`); | |
console.log(`========= Success requests: ${requestsSuccess}`); | |
} catch(e) { | |
console.log(e); | |
} finally { | |
console.log(`========= time ${(new Date() - START_TIME)/1000}s =========`); | |
process.exit(); | |
} | |
})(); |
This file contains 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
// link to API (just sign up to get the key ;)): | |
// https://rapidapi.com/divad12/api/numbers-1 | |
const https = require('https'); | |
//======================= jsonGet Function =======================// | |
function jsonGet (day=1, month=0){ | |
const options = { | |
host: 'numbersapi.p.rapidapi.com', | |
port: 443, | |
path: `/${month}/${day}/date?fragment=true&json=true`, | |
headers: { | |
"X-RapidAPI-Host": "numbersapi.p.rapidapi.com", | |
"X-RapidAPI-Key": "YOUR_KEY" | |
} | |
}; | |
return new Promise((resolve, reject) => { | |
https.get(options, function(res) { | |
let body = ''; | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
resolve(body); | |
}); | |
}).on('error', function(e) { | |
reject(e); | |
}); | |
}) | |
} | |
function getDaysArray(startDate, endDate) { | |
let dateArray = []; | |
for( let dt=startDate; dt<=endDate; dt.setDate(dt.getDate()+1)){ | |
const date = new Date(dt); | |
dateArray.push({day: date.getDate(), month: date.getMonth()}); | |
} | |
return dateArray; | |
} | |
async function getAllPrograms(callback) { | |
const YEARS = 10; | |
const startDate = new Date(new Date().setFullYear(new Date().getFullYear() - YEARS)); | |
const endDate = new Date(); | |
const dates = getDaysArray(startDate, endDate); | |
let counter = 0; | |
// ============ sync LOOP ============== // | |
// -----> time 168.973s - 1y | |
// -----> time 1756.472s - 10y | |
// for (let i=0; i<dates.length; i++) { | |
// try { | |
// const response = await jsonGet(dates[i].day, dates[i].month); | |
// console.log(`${dates.length -i} requests left`); | |
// console.log(response); | |
// } catch (e) { | |
// console.log(e); | |
// } | |
// counter ++; | |
// if (counter === dates.length) { | |
// callback(); | |
// } | |
// } | |
// ============ sync RECURSIVE ============== // | |
// -----> time 176.526s - 1y, timeOffset = 10 | |
// -----> time 1943.451s - 10y, timeOffset = 10 | |
// const timeOffset = 10; | |
// (function myLoop (i) { | |
// setTimeout(async function () { | |
// try { | |
// const response = await jsonGet(dates[i].day, dates[i].month); | |
// console.log(`${i} requests left`); | |
// console.log(response); | |
// } catch (e) { | |
// console.log(e) | |
// } | |
// counter ++; | |
// if (counter === dates.length) { | |
// callback(); | |
// } else { | |
// i--; | |
// myLoop(i); | |
// } | |
// }, timeOffset) | |
// })(dates.length -1); | |
// ============ sync RECURSIVE with STEP ============== // | |
// -----> time 10.17s - 1y, timeOffset = 20, step = 20 | |
// -----> time 108.541s - 10y, timeOffset = 20, step = 20 | |
// -----> time 38.558s - 10y, timeOffset = 20, step = 100 | |
const timeOffset = 20; | |
const step = 100; | |
(function myLoop (i) { | |
setTimeout(async function () { | |
try { | |
const start = (i+1-step > 0) ? i+1-step : 0; | |
const end = (i+1 <= dates.length) ? i+1 : dates.length; | |
console.log(`Start number in group: ${start}`); | |
console.log(`Last number: ${end}`); | |
const shopGroup = dates.slice(start, end); | |
const shopReadFunc = shopGroup.map(async date => { | |
const response = await jsonGet(date.day, date.month); | |
console.log(response); | |
}); | |
await Promise.all(shopReadFunc); | |
} catch (e) { | |
console.log(e) | |
} | |
counter += step; | |
if (counter >= dates.length) { | |
callback(); | |
} else { | |
i-=step; | |
myLoop(i); | |
} | |
}, timeOffset) | |
})(dates.length -1); | |
// ============ async LOOP ============== // | |
// -----> 2s - 1y | |
// -----> DEAD 10y | |
// const shopReadFunc = dates.map(async (date, i) => { | |
// try { | |
// const response = await jsonGet(dates[i].day, dates[i].month); | |
// console.log(`${dates.length - i} requests left`); | |
// console.log(response); | |
// } catch (e) { | |
// console.log(e) | |
// } | |
// counter ++; | |
// if (counter === dates.length) { | |
// callback(); | |
// } | |
// }); | |
// | |
// await Promise.all(shopReadFunc); | |
} | |
const START_TIME = new Date(); | |
process.stdout.write('Test start'); | |
getAllPrograms(function(err){ | |
if (err) console.log(err); | |
console.log(`============================ time ${(new Date() - START_TIME)/1000}s ============================`); | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment