Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Using any for the return type of item may incur a performance hit #7

Open
bzbarsky opened this issue Jul 21, 2020 · 9 comments
Open

Comments

@bzbarsky
Copy link

The change to use any(to allow undefined) will mean some deoptimization of the callers of item, because they will not be able to obtain any (JIT) compile-time type information, whereas right now they can do just that.

@ljharb
Copy link
Member

ljharb commented Jul 21, 2020

I'm confused - any isn't the same thing as "allow undefined" unknown works the same, and a TS type signature for the item method could certainly accept a generic or infer the contents' type from the receiver.

@bzbarsky
Copy link
Author

Right now in Web IDL the only type that allows undefined is any. This is specifically about the IDL+ES behavior; TS is not really relevant here as far as I can tell.

@ljharb
Copy link
Member

ljharb commented Jul 21, 2020

ah, i misunderstood, thanks for clarifying.

does that mean that anything in HTML that returns undefined is prohibited by the spec from optimizing based on the possible return type, but returning null is fine?

@bzbarsky
Copy link
Author

First, very few things in HTML return undefined.

Second, it's not a matter of "prohibited". It's just that you have no idea what the return type must be, because it's literally any in the IDL. You can still do run-time observation of the types, of course.

With null, you can express the type "a Node or null", for example, in IDL by writing Node? and then you can be sure that the returned value will never be, say, a string and might be able to optimize accordingly at compile time.

IDL could be extended to allow expressing something like (Node or undefined) to help address this, of course; I was just describing the current state of things.

@ljharb
Copy link
Member

ljharb commented Jul 21, 2020

Makes sense. Since very few things in JS return null, and most return undefined, it seems like a pretty critical gap in IDL, but I definitely don't have any context around that.

@styfle
Copy link
Contributor

styfle commented Jul 21, 2020

IDL could be extended to allow expressing something like (Node or undefined) to help address this

That seems like a worthy endeavor! Is there a issue tracker for IDL to request that change?

@bzbarsky
Copy link
Author

whatwg/webidl#60 exists already, fwiw.

@tabatkins
Copy link
Collaborator

Yeah, we just need to fix that issue in WebIDL. It's not hard, just needs to be carried to completion.

@Jack-Works
Copy link
Member

but that PR has opened for 5 years...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants