google / google/closure-compiler

Optimization: label not needed

Open
#1,995 2 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

Hey, I'm using this version:

``` shell
--version
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20160517
Built on: 2016/05/18 16:21
```

and my example program is:

``` js
label: for(var i = 0; i < 10; i++) {
if (i == 1) {
continue label;
}
console.log(i);
}
```

which compiles to:

``` js
var i=0;a:for(;10>i;i++){if(1==i)continue a;console.log(i)};
```

Notice that `continue label` was translated to `continue a`. Since the target of a bare `continue` statement would be the same as `continue label` in this case, `a` could be omitted.

I know it might be silly to save two bytes on a case that doesn't happen very often, but I figured I'd document the behaviour in case it's a new observation.

I think the idea could be generalized to, "labels can be omitted from continue statements when the label is equal to the current loop" AND "labels can be omitted from break statements when the label is equal to the current loop, switch, or label"... Something like that.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.