Skip to content

Instantly share code, notes, and snippets.

@barraponto
Created July 6, 2022 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barraponto/06aee9512916d8043eb09099308b6114 to your computer and use it in GitHub Desktop.
Save barraponto/06aee9512916d8043eb09099308b6114 to your computer and use it in GitHub Desktop.
const { performance } = require("perf_hooks");
const { setTimeout } = require("timers/promises");
function timeit(start, end) {
console.log(`Execution time: ${start - end} ms.`);
}
async function parallelPromises(promiseA, promiseB, promiseC) {
const start = performance.now();
const results = [await promiseA, await promiseB, await promiseC];
console.log(results);
const end = performance.now();
timeit(start, end);
}
async function parallelAll(promiseA, promiseB, promiseC) {
const start = performance.now();
const results = await Promise.allSettled([promiseA, promiseB, promiseC]);
console.log(results);
const end = performance.now();
timeit(start, end);
}
parallelPromises(setTimeout(3000, 1), setTimeout(2000, 2), setTimeout(1000, 3));
parallelAll(setTimeout(3000, 1), setTimeout(2000, 2), setTimeout(1000, 3));
async function sequential(args1, args2, args3) {
const start = performance.now();
const results = [
await setTimeout(...args1),
await setTimeout(...args2),
await setTimeout(...args3),
];
console.log(results);
const end = performance.now();
timeit(start, end);
}
sequential([3000, 1], [2000, 2], [1000, 3]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment