$ node -v
v12.12.0
$ node promise.js
pure: 4.863ms
promise: 47.649ms
Created
November 21, 2019 10:50
-
-
Save koh110/6650b75b055ff0e880954d1576a78d71 to your computer and use it in GitHub Desktop.
promiseラップするとどれくらい遅くなるか
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 len = 10000 | |
function pure() { | |
console.time('pure') | |
const get = () => { | |
return { foo: 'bar' } | |
} | |
for (let i = 0; i < len; i++) { | |
get() | |
} | |
console.timeEnd('pure') | |
} | |
async function promise() { | |
console.time('promise') | |
const get = () => { | |
return Promise.resolve({ foo: 'bar' }) | |
} | |
for (let i = 0; i < len; i++) { | |
await get() | |
} | |
console.timeEnd('promise') | |
} | |
async function main() { | |
pure() | |
await promise() | |
} | |
main().catch(e => { | |
console.error(e) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment