Skip to content
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

Use for .. in to iterate over arrays. NFC #15094

Closed
wants to merge 1 commit into from
Closed

Use for .. in to iterate over arrays. NFC #15094

wants to merge 1 commit into from

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Sep 22, 2021

This seems to save some code size.. are there any downsides?

This seems to save some code size.. are there any downsides?
@sbc100 sbc100 requested a review from juj September 22, 2021 16:11
@sbc100
Copy link
Collaborator Author

sbc100 commented Sep 22, 2021

Of course once we get to https://github.com/emscripten-core/emscripten/issues/11984 we can use const and for .. of.. but this seems like a reasonable intermediate step.

@sbc100 sbc100 requested a review from RReverser September 22, 2021 16:13
@RReverser
Copy link
Collaborator

This at least used to have worse performance in modern engines than a C-style for loop for arrays, since it has to check any properties and not just integer indices.

Personally, I'd probably keep the old version. It might be useful to see how much this saves on practice (after gzip/brotli) on some real-world example, but I doubt it's much.

@sbc100
Copy link
Collaborator Author

sbc100 commented Sep 22, 2021

OK sounds good. I'll close this for now.

Would using for .. of .. help once we have ES6 by default?

@sbc100 sbc100 closed this Sep 22, 2021
@sbc100 sbc100 deleted the for_loops branch September 22, 2021 17:01
@juj
Copy link
Collaborator

juj commented Sep 22, 2021

for ... in iterates over the array differently compared to the C style for loop. The difference happens when the prototype of Array is modified:

image

I used to be a big fan of for ... in, but people kept raising bug reports about different frameworks expanding Array.prototype for new functionality, which would break compatibility with for ... in.

So to be safe, we should never use for ... in to iterate over arrays, since we don't know what kind of environment the code will be executing in. (I could entertain a -s UNMODIFIED_ARRAY_PROTOTYPE=0/1 option, but I already can hear how people will hate the idea :D )

for ... of does not have the above problem (it won't print asdf). I think it would be good to have an internal preprocessor flag to allow using ES6 constructs. The spread syntax ... is another one that will give a nice code size saving as well.

Although yeah it's good to note that both for ... of and ... spread syntax do have a microbenchmark history of being worse in performance. Though this would only matter in WebGL library maybe, all other places are generally very perf agnostic.

@RReverser
Copy link
Collaborator

I think it would be good to have an internal preprocessor flag to allow using ES6 constructs. The spread syntax ... is another one that will give a nice code size saving as well.

I agree, same for various class constructs and object shorthands - we certainly have a bunch of places where those could help.

@RReverser
Copy link
Collaborator

Maybe we could enable those as part of the new ESM mode discussed in issue about ES6 exports. I think it would be reasonable to tie the two together.

@sbc100
Copy link
Collaborator Author

sbc100 commented Sep 22, 2021

Maybe we could enable those as part of the new ESM mode discussed in issue about ES6 exports. I think it would be reasonable to tie the two together.

I have a plan for how to enable ES6 across the codebase: #11984 . Basically I hope to enable ES6 by default and then force transpilation when users opt into older browser support. One key pointer is that we would like the default settings not to require transpilation which means the default broswer support settings need to have ES6 support (which I suspect they already do).

@RReverser
Copy link
Collaborator

Thanks, the plan in that issue looks solid, and I like the idea of just assuming that Wasm-supporting browsers have ES6 anyway. Btw, the stale bot just marked that issue as stale, you might want to bump it up (or resolve 😛).

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

Successfully merging this pull request may close these issues.

3 participants