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

variable could be assigned at declaration #3783

Open
Enideo opened this issue Mar 16, 2021 · 1 comment
Open

variable could be assigned at declaration #3783

Enideo opened this issue Mar 16, 2021 · 1 comment

Comments

@Enideo
Copy link

Enideo commented Mar 16, 2021

I believe the output from advanced mode could be (mildly) optimised from var a;a=window.setTimeout; to var a=window.setTimeout; without causing any issues.

The full input file was:

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// ==/ClosureCompiler==

function hello(name) {
  alert('Hello, ' + name);
}

var timer;
if( true ){ // or a truthy variable
  timer = window['setTimeout'];
}

timer(function(){ hello('New user'); },100);
timer(function(){ hello('something else'); },100);

producing

var a;a=window.setTimeout;a(function(){alert("Hello, New user")},100);a(function(){alert("Hello, something else")},100);
@blickly
Copy link
Contributor

blickly commented Mar 17, 2021

This definitely sounds like a safe optimization to make. Unfortunately, there are so many safe optimziations that would improve code-size that we don't generally have time to act on them all. I think Nick Santos used to jokingly refer people to the Full Employment Theorem on this topic, which basically says there are always more optimizations to write than there is time to implement them.

If this is something you'd like to work on yourself, I'd guess it can be implemented as a "Peephole Optimization".

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

No branches or pull requests

2 participants