Rich-Harris / butternut

The fast, future-friendly minifier
https://butternut.now.sh
MIT License
1.17k stars 17 forks source link

Inline values #104

Open Rich-Harris opened 7 years ago

Rich-Harris commented 7 years ago

If a variable has a literal value then in many cases it makes sense to inline the value, rather than declaring the variable:

// input
var COUNT = 99;
for ( var i = 0; i < COUNT; i += 1 ) console.log(i);

// output
var COUNT=99;for(var i=0;i<COUNT;i+=1)console.log(i)

// better output
for(var i=0;i<99;i+=1)console.log(i)

This is always true when the variable is only referenced once. If it's referenced multiple times, then it depends on the value.

Next level: inlining values that aren't part of the variable initialiser:

var COUNT;
COUNT = 99;
for ( var i = 0; i < COUNT; i += 1 ) console.log(i);