Skip to content

Instantly share code, notes, and snippets.

@noseratio
Last active June 29, 2021 07:00
Show Gist options
  • Save noseratio/76780faeed091c729f2fdba3abff5abf to your computer and use it in GitHub Desktop.
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
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