0

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.

  • I can't seem to replicate this error. Have a look at this fiddle: https://jsfiddle.net/9w30pev1/1/ (Go on to the JavaScript Tab to see the code). It seems like Promise.all is faster, but it is also consistent whether you do it first or second, and whether you do it in an async funciton and await it or not. – Krokodil Sep 05 '21 at 13:02
  • DNS lookup maybe? – CherryDT Sep 05 '21 at 13:10
  • @CherryDT, maybe but what that, in theory, should be pseudo-random to us, humans. So if his error is consistent in nature, I don't know what the problem may be. – Krokodil Sep 05 '21 at 13:11
  • My point was that the first request may trigger a lookup and everything following it can use the cached result. Even more, the site could reuse connections so the second part won't have the overhead of establishing a new connection to the server... – CherryDT Sep 05 '21 at 13:26
  • It could also be a similar situation on the server side - the second requests using cached resources of the first – CherryDT Sep 05 '21 at 13:28
  • @AlphaHowl i moved Promise.all code from function2 in your sandbox into function1 and depending on which block of code comes first i get different results. Seems like this problem occurs only when both expamples are placed in one function body. Promise.all works slower (~20ms) if placed firts, at least form my observation on your sandbox. – nonamenoname Sep 05 '21 at 13:40
  • @CherryDT i didn't think about caching on the server side, this makes sense but, i still have this tiny difference in the sandbox https://jsfiddle.net/9w30pev1/1/ if both examples are inside one function body. – nonamenoname Sep 05 '21 at 13:45
  • 1
    Depends entirely on what `mainPageAPI` is and what its methods do. Maybe they are queued and cached internally. – Bergi Sep 05 '21 at 14:37
  • @pilchard The first block with the linear `await`s will also throw if any of the promises reject, so it is equivalent to `Promise.all`. And using `Date` for measuring is fine, OP is reporting times in the order of hundreds of milliseconds. – Bergi Sep 05 '21 at 14:39
  • By the looks of it, I think @Bergi has nailed the answer. – Krokodil Sep 05 '21 at 17:10
  • @AlphaHowl I don't think any of these answers address the real reason. My experience is that HTTP calls will open a TCP connection and that connection stays alive and enables subsequent calls to resolve faster. You can view this on the network tab of a browser. The first network call to the API will always be the longest. OP can probably verify with a screenshot on the network tab – Daniel Duong Sep 06 '21 at 06:33
  • 1
    @DanielDuong We don't even know whether the OP is doing HTTP calls or anything network-related. – Bergi Sep 06 '21 at 08:17
  • @Bergi It says in the title. Fetch calls. – Daniel Duong Sep 07 '21 at 07:04

0 Answers0