I'm currently gathering extensive data from an API using an async function that iterates through a large amount of information using a loop. I'm sending about 100 requests and it's taking approximately 8 seconds to complete.
Are there any techniques or methods that I could implement to optimize the speed of my script?
async function getPlayerData() {
// Retrieve current room information
const response = await fetch(url);
let teams = await response.json();
let players = teams.players;
let playerArray = players.length;
for (var i = 0; i < playerArray; i++) {
let username = players[i].username;
let userId = players[i].id;
// Retrieve user matches
const userMatch = await fetch(`https://api.site.com/user_profile/get_latest_matches?_=&id=${userId}&page=1`);
let matchList = await userMatch.json();
let matchListArray = matchList.length;
for (var ii = 0; ii < matchListArray; ii++) {
// Retrieve match statistics
const matchListResponse = await fetch(`https://api.site.com/match/get?_=&id=${matchList[ii].id}`);
let matchListResponseStats = await matchListResponse.json();
async function matchData() {
if (matchListResponseStats.players === null) {
const kills = 0;
const deaths = 0;
const headshot = 0;
const headshotProc = 0;
return [kills, deaths, headshotProc, headshot];
} else {
const filterArray = matchListResponseStats.players[i];
console.log(filterArray);
console.log(filterArray.kills);
console.log(filterArray.deaths);
console.log(filterArray.headshots);
}
}
matchData();
}
}
}
getPlayerData();