Siorki / RegPack

Self-contained packer for size-constrained JS code
Other
301 stars 14 forks source link

One little optimization for "refactor setInterval" feature #72

Closed xem closed 7 years ago

xem commented 7 years ago

Packing this example code with setInterval refactor and loop var "t":

t=0;setInterval(function(e){t++},33)

preprocessed code is:

if(!f){}f++

should be simplified to:

f++

packed code is:

for(_='if(!f){}f++';G=/[]/.exec(_);)with(_.split(G))_=join(shift(f=0));setInterval(_,33)

should be simplified to:

for(_='f++';G=/[]/.exec(_);)with(_.split(G))_=join(shift(f=0));setInterval(_,33)

cheers!

Siorki commented 7 years ago

You mean you do not have any initialization code ? That is, code that is run once before entering the loop. Is is an example from a real demo ?

I did not expect that case to happen, but it should be easy to solve.

xem commented 7 years ago

My real-life demo is very near from the example I sent above:

t=0;setInterval(function(){t?play():init();t++},33)

(with "play()" and "init()" replaced by big chunks of code.)

unfortunately, this packs as:

for(_='if(!t){}t?play():init();t++';G=/[]/.exec(_);)with(_.split(G))_=join(shift(t=0));setInterval(_,33)

I'm not asking you to support ternaries a?b:c, but if you could remove the if(!t){} fragment when the braces are empty, that'd be great.

Siorki commented 7 years ago

Removed the code if(!f){} if there is no initialization code, neither at the beginning nor the end. Adapted thermal view as well.