Last active
October 4, 2022 04:20
-
-
Save feightwywx/e1c1328819469bb78dcd35c1e5c0163c to your computer and use it in GitHub Desktop.
Gist for www.direcore.xyz/archives/35/
This file contains 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 numbers = [1, 2, 3, 4, 5] | |
let start = Date.now() | |
async function someRandomWork() { | |
for (let i = 0; i < 2e9; i++) { | |
} | |
} | |
async function getCurrentTime() { | |
return new Date(Date.now() - start) | |
.toISOString() | |
.split('T')[1] | |
.split('Z')[0] | |
} | |
async function mapAsync() { | |
await Promise.all(numbers.map(async i => { | |
await someRandomWork(); | |
console.log(await getCurrentTime(), i); | |
})) | |
console.log('mapAsync finished!') | |
} | |
async function reduceAsync() { | |
await numbers.reduce(async (promise, i) => { | |
await promise; | |
await someRandomWork(); | |
console.log(await getCurrentTime(), i); | |
}, Promise.resolve()); | |
console.log('reduceAsync finished!') | |
} | |
async function forOfAsync() { | |
for (const i of numbers) { | |
await someRandomWork(); | |
console.log(await getCurrentTime(), i); | |
} | |
console.log('forOfAsync finished!') | |
} | |
async function forAwaitOfAsync() { | |
for await (const i of numbers) { | |
await someRandomWork(); | |
console.log(await getCurrentTime(), i); | |
} | |
console.log('forAwaitOfAsync finished!') | |
} | |
async function main() { | |
console.log(`\nmap test`); | |
start = Date.now(); | |
await mapAsync(); | |
console.log(`\nreduce test`); | |
start = Date.now(); | |
await reduceAsync(); | |
console.log(`\nfor...of test`); | |
start = Date.now(); | |
await forOfAsync(); | |
} | |
main(); | |
// expected output: | |
// | |
// map test | |
// 00:00:03.863 1 | |
// 00:00:03.863 2 | |
// 00:00:03.863 3 | |
// 00:00:03.863 4 | |
// 00:00:03.863 5 | |
// mapAsync finished! | |
// reduce test | |
// 00:00:00.629 1 | |
// 00:00:01.256 2 | |
// 00:00:01.883 3 | |
// 00:00:02.510 4 | |
// 00:00:03.137 5 | |
// reduceAsync finished! | |
// for...of test | |
// 00:00:00.627 1 | |
// 00:00:01.255 2 | |
// 00:00:01.881 3 | |
// 00:00:02.507 4 | |
// 00:00:03.134 5 | |
// forOfAsync finished! |
This file contains 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 numberPromises = [ | |
Promise.resolve(1), | |
Promise.resolve(2), | |
Promise.resolve(3), | |
Promise.resolve(4), | |
Promise.resolve(5) | |
] | |
let start = Date.now() | |
async function someRandomWork() { | |
for (let i = 0; i < 2e9; i++) { | |
} | |
} | |
async function getCurrentTime() { | |
return new Date(Date.now() - start) | |
.toISOString() | |
.split('T')[1] | |
.split('Z')[0] | |
} | |
async function forOfAsync() { | |
for (const i of numberPromises) { | |
await someRandomWork(); | |
console.log(await getCurrentTime(), i); | |
} | |
console.log('forOfAsync finished!') | |
} | |
async function forAwaitOfAsync() { | |
for await (const i of numberPromises) { | |
await someRandomWork(); | |
console.log(await getCurrentTime(), i); | |
} | |
console.log('forAwaitOfAsync finished!') | |
} | |
async function main() { | |
console.log(`\nfor...of test`); | |
start = Date.now(); | |
await forOfAsync(); | |
console.log(`\nfor await...of test`); | |
start = Date.now(); | |
await forAwaitOfAsync(); | |
} | |
main(); | |
// expected output: | |
// | |
// for...of test | |
// 00:00:00.988 Promise { 1 } | |
// 00:00:01.933 Promise { 2 } | |
// 00:00:02.562 Promise { 3 } | |
// 00:00:03.189 Promise { 4 } | |
// 00:00:03.816 Promise { 5 } | |
// forOfAsync finished! | |
// for await...of test | |
// 00:00:00.627 1 | |
// 00:00:01.254 2 | |
// 00:00:01.882 3 | |
// 00:00:02.509 4 | |
// 00:00:03.136 5 | |
// forAwaitOfAsync finished! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment