Last active
June 29, 2021 07:00
-
-
Save noseratio/76780faeed091c729f2fdba3abff5abf to your computer and use it in GitHub Desktop.
Exposing a "thenable" can be ~2 times slower than exposing a promise directly
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
void async function() { | |
function createDeferred() { | |
let resolve, reject; | |
const promise = new Promise((...args) => [resolve, reject] = args); | |
return Object.freeze({ | |
resolve, | |
reject, | |
promise, | |
then: promise.then.bind(promise) | |
}); | |
} | |
console.time("deferred"); | |
for (let n = 0; n < 20000000; n++) { | |
const d = createDeferred(); | |
d.resolve(true); | |
// ~2 times slower than `await d.promise` | |
await d; | |
} | |
console.timeEnd("deferred"); | |
}().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment