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

[plugin-transform-for-of] Ability to opt in or out of for-of loose mode #9915

Open
trusktr opened this issue Apr 27, 2019 · 2 comments
Open

Comments

@trusktr
Copy link

trusktr commented Apr 27, 2019

Feature Request

Is your feature request related to a problem? Please describe.

Loose mode is nice, even though my target environments support for-of, I'd like to keep using loose mode to have faster iteration on arrays with convenient for-of syntax.

But sometimes, in rare cases, I may want to take advantage of native for-of in order to edit arrays in place while iterating (including adding or deleting items), but this doesn't work in loose mode for obvious reasons.

Describe the solution you'd like

It'd be nice to have a comment that can enable (or disable) loose for-of. F.e. If we're in loose mode, we could write the following to enable strict mode (it may compile, or may simply output the for-of as-is depending on env config):

// @babel no-loose
for (const item of array) {
  // ...
}

Not sure about the comment format, but you get the idea.

Conversely, someone with loose mode turned off might want to do

// @babel loose
for (const item of array) {
  // ...
}

The no-* could be a generic commenting mechanism to disable/enable some feature.

Describe alternatives you've considered

I can opt for the strict mode by converting an array into a Set, for example. But it seems clunky.

Teachability, Documentation, Adoption, Migration Strategy

...


Sidenote, in my tests, the following hand-written version I've been using,

var arr = [1, 2, 3];
for (let i = 0, l = arr.length; i < l; i+=1) {
  var a = arr[i];
}

is almost three times faster than Babel's output.

var _arr = [1, 2, 3];
for (var _i = 0; _i < _arr.length; _i++) {
  var a = _arr[_i];
}
@babel-bot
Copy link
Collaborator

Hey @trusktr! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community
that typically always has someone willing to help. You can sign-up here
for an invite.

@trusktr
Copy link
Author

trusktr commented Apr 27, 2019

Here's a test that shows the speed difference:

var arr = Array.from({length: 1000000});

var n = 0
var start = performance.now()
for (var i = 0; i < arr.length; i++) {
  n+=1
}

console.log('test 1 time:', performance.now() - start)

var n = 0
var start = performance.now()
for (var i = 0, l = arr.length; i < l; i+=1) {
  n+=1
}

console.log('test 2 time:', performance.now() - start)

// Output:
// test 1 time: 8.255000007920898
// test 2 time: 2.970000001369044

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

No branches or pull requests

2 participants