Skip to content

Instantly share code, notes, and snippets.

@EtherDream
Last active May 7, 2022 03:29
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 EtherDream/52649e4939008e149d0cb3a944c055b7 to your computer and use it in GitHub Desktop.
Save EtherDream/52649e4939008e149d0cb3a944c055b7 to your computer and use it in GitHub Desktop.
(function() {
'use strict'
async function pending() {
return 11
}
function mayPending() {
if (Math.random() < 0.001) {
return pending()
}
return 22
}
async function main() {
console.time('s1')
for (let i = 0; i < 1e6; i++) {
const val = await mayPending()
}
console.timeEnd('s1')
console.time('s2')
for (let i = 0; i < 1e6; i++) {
const ret = mayPending()
const val = ret.then ? await ret : ret
}
console.timeEnd('s2')
}
main()
})()
@EtherDream
Copy link
Author

EtherDream commented May 4, 2021

s1: 2412.416015625 ms
s2: 20.87890625 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment