Created
July 6, 2022 22:21
-
-
Save barraponto/06aee9512916d8043eb09099308b6114 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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