-
Notifications
You must be signed in to change notification settings - Fork 28
Should PromiseResolve(C, x) return x if x is a promise? #37
Comments
Based on these numbers, that looks like it's 0.00127ms slower for a single operation - that doesn't seem particularly large, but I'm not used to evaluating benchmarks like this. Separately, I grant that it's possible to construct a scenario such that the ordering is observable - but is is likely to be observable in the majority case? If not, is that something that could be detected and optimized away? Having Promises be fast is very valuable, but the "fast path" in |
I like this proposal. It seems consistent with the current API, not harmful for programmers, and reduces the implementation burden to get as good performance. As @gsathya explains, it's rather difficult to optimize these things away completely because of microtask queue ordering. Promise performance remains a source of complaints/fear for developers, even after it has improved in V8. @ljharb Can you explain more about why you think the decision with Promise.resolve is an unfortunate decision? Even if that decision is unfortunate, it's hard for me to see how developers would encounter and be harmed by the optimization in this more specific case. |
It is indeed consistent, and I don't think it's particularly harmful. I'm not strongly opposed to making this change. I think it's unfortunate because a) it's codifying a semantic change in the spec solely for optimization reasons; b) it means |
Yes, both of those "unfortunate" things are true. That is exactly what the design is. Do you have an idea of when users might want to depend on b) ? In particular, I think throwing/not throwing is a bit more drastic and consequential than equal/not equal. |
I agree - in the case of throwing/not throwing, if i make a mistake, i get an exception loudly notifying me; in the case of returning the wrong value, my code silently does the wrong thing. As to when users might depend on b), I know that i would do it (if i didn't know about this Promise.resolve spec fast path) if i wanted to attach extra properties to a promise, or to use it as a key in a |
OK, I can (abstractly) see that cloning use case. Here, |
That's a very fair point; my distaste for what I'll make this change now. Thanks @gsathya and @littledan for talking it through! |
Promise.resolve(x)
returnsx
ifx
a promise (and same subclass), butPromiseResolve
does not have this check. This means, we always create a new promise irrespective of whetherx
is a promise or not. This behavior is observable because of the microtask queue ordering.Running the above microbenchmark in V8 --
Current spec implementation: 570.32 ms
Modified implementation which does not create an extra promise: 443.42 ms
The text was updated successfully, but these errors were encountered: