Inside a function i have two blocks of code where i am trying to measure processing time of my api calls. Here's the code:
const start1 = new Date().getTime();
const trendingMoviesDay = await mainPageAPI.getTrendingMovies(true);
const trendingMoviesWeek = await mainPageAPI.getTrendingMovies();
const trendingTVDay = await mainPageAPI.getTrendingTV(true);
const trendingTVWeek = await mainPageAPI.getTrendingTV();
const popularMovies1 = await mainPageAPI.getPopularMovies(1);
const popularMovies2 = await mainPageAPI.getPopularMovies(2);
const popularTV1 = await mainPageAPI.getPopularTV(1);
const popularTV2 = await mainPageAPI.getPopularTV(2);
const end1 = new Date().getTime();
console.log(end1 - start1);
const start2 = new Date().getTime();
const results = await Promise.all(
[
mainPageAPI.getTrendingMovies(true),
mainPageAPI.getTrendingMovies(),
mainPageAPI.getTrendingTV(true),
mainPageAPI.getTrendingTV(),
mainPageAPI.getPopularMovies(1),
mainPageAPI.getPopularMovies(2),
mainPageAPI.getPopularTV(1),
mainPageAPI.getPopularTV(2)
]
);
const end2 = new Date().getTime();
console.log('promiseAll',end2 - start2);
In this case first console.log shows 650-850 ms on average, and 2nd - only 50-100ms, but if i swap around these two blocks the first one (promise.all) will be 500-700ms on average and second 100-250ms. What am i missing here? (probably a lot) I would appreciate if anyone could explane these results.