-
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perfomance: promiseState optimization #37
Comments
Unfortunately, that doesn't pass the following test Deno.test(
"promiseState() returns 'fulfilled' for resolved promise (with null)",
async () => {
const p = Promise.resolve(null);
assertEquals(await promiseState(p), "fulfilled");
},
); |
This version passes all the tests and wins by 3000x performance export function promiseState2(p: Promise<unknown>): Promise<PromiseState> {
const t = {};
return Promise.resolve().then(() =>
Promise.race([p, t]).then(
v => (v === t ? 'pending' : 'fulfilled'),
() => 'rejected'
)
);
} |
If you use Symbol as a value to check that it gives an upper 5-10% to the previous version const t = Symbol.for('t');
export function promiseState3(p: Promise<unknown>): Promise<PromiseState> {
return Promise.resolve().then(() =>
Promise.race([p, t]).then(
v => (v === t ? 'pending' : 'fulfilled'),
() => 'rejected'
)
);
} |
|
Nice. Using |
may be use to have a unique symbol ? const t = Symbol('t');
export function promiseState3(p: Promise<unknown>): Promise<PromiseState> {
return Promise.resolve().then(() =>
Promise.race([p, t]).then(
v => (v === t ? 'pending' : 'fulfilled'),
() => 'rejected'
)
);
} |
Unfortunately, that doesn't pass current test... |
I'm sorry but I don't get your point. The test code you've written seems expected and the new implementation doesn't pass that test. |
So, basically, are you saying that it's more about making the behavior more intuitive rather than just improving performance? |
It sounds the other way around. Since the current implementation includes internal handling that waits for the Promise to resolve, it seems more fitting to describe your implementation, which skips the waiting, as "immediate." That said, P.S. I updated the PR. |
I'm thinking of doing it on Symbol('t') and it will do in principle. We'll, make benchmarks for the implementation of but {} and for Symbol("). But I still think that by default the entire event loop should not wait, and in tests you can also pass a parameter so that it waits. |
I understand the sentiment, and I would prefer to do that as well, but I feel that it’s not a strong enough reason to introduce a breaking change. If asyncutil were still in the 0.x.y stage, I would have implemented it, but since it’s already at version 1.x.y, I want to avoid making changes that could break backward compatibility as much as possible. |
An alternative option is that add |
Okay, then let's better add a separate method currentPromiseState or peekPromiseState, which would not expect an event loop. But then you need to describe both implementations in the documentation and examples and where it is better to use which one |
Of course, I’ll document it, but the nuance is a bit different. I plan to treat the current |
Yes, I support this decision. |
Benchmarks
#Tests
The text was updated successfully, but these errors were encountered: