Skip to content

Instantly share code, notes, and snippets.

@koh110
Created November 21, 2019 10:50
Show Gist options
  • Save koh110/6650b75b055ff0e880954d1576a78d71 to your computer and use it in GitHub Desktop.
Save koh110/6650b75b055ff0e880954d1576a78d71 to your computer and use it in GitHub Desktop.
promiseラップするとどれくらい遅くなるか
$ node -v
v12.12.0

$ node promise.js
pure: 4.863ms
promise: 47.649ms
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