metasync icon indicating copy to clipboard operation
metasync copied to clipboard

Update parallel benchmarks (Promise + Compose)

Open lundibundi opened this issue 5 years ago • 2 comments

  • Don't create object every time in Promise resolve
  • Collect results in an array in Compose bench as it is dome in Promise.all

lundibundi avatar Dec 05 '18 20:12 lundibundi

We need to use queueMicrotask for promisess too because new Promise(resolve => resolve(value)) don't queue microtasks, it returns resolved promise immediately. You can check this by following example:

const f = callback => queueMicrotask(() => callback(10));
f(res => {
  console.log(res);
});

const p = new Promise(resolve => resolve(10));
console.dir({ p });

Output will be:

{ p: Promise { 10 } }
(node:32587) ExperimentalWarning: queueMicrotask() is experimental.
10

If we add queueMicrotask to promise:

const f = callback => queueMicrotask(() => callback(10));
f(res => {
  console.log(res);
});

const p = new Promise(resolve => queueMicrotask(() => resolve(10)));
p.then(res => {
  console.dir({ res });
});

Output will be:

10
{ res: 10 }

Now two cases are equal priority, so if we swap calls results will swap too:

const f = callback => queueMicrotask(() => callback(10));
const p = new Promise(resolve => queueMicrotask(() => resolve(10)));

p.then(res => {
  console.dir({ res });
});

f(res => {
  console.log(res);
});

Output will be:

{ res: 10 }
10

tshemsedinov avatar Jan 08 '19 12:01 tshemsedinov

ping @tshemsedinov

lundibundi avatar Jun 16 '19 17:06 lundibundi