Skip to content

ivanhuay/micron-test-for-await

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Micron Benchmark For await vs await

This is a benchmark to analize the performance between the two functions for + await vs for-await.

Motivation

In this benchmark we measure the performance variation using this two funcions:

for (var i = 0; i < limit; i++) {
  let elm = await getResource();
}

vs

data = {
  [Symbol.asyncIterator]() {
    return {
      i: 0,
      next() {
        if (this.i < limit) {
          this.i++;
          return getResource()
              .then((value)=>({value, done: false}));
        }
        return Promise.resolve({ done: true });
      }
    };
  }
};
for await (let prom of data) {
}

for await looks simpler but you have to use an AsyncIterator that isn't simpler than using an array.

Results

Both functions have similar performance but for await has more complexity because of the AsyncIterator. Only if we were using an async iterator previously the for next is better than a simple for + await.

view results HERE Alt image

run the test

git clone https://github.com/ivanhuay/micron-test-for-await.git

cd micron-test-for-await

npm i

npm run test

after running that the results should be on the docs folder.