UglifyJS icon indicating copy to clipboard operation
UglifyJS copied to clipboard

Feature suggestion: automatically extract `symbol|constants` that may reduce the code size into the variables declared by let

Open steve02081504 opened this issue 1 year ago • 1 comments

(I've only recently delved a little into js, so feel free to point out where there's a problem) Consider the following code:

//Initialize all sstp operations
let sstp_version_table = {
	SEND: "1.4",
	NOTIFY: "1.1",
	COMMUNICATE: "1.1",
	EXECUTE: "1.2",
	GIVE: "1.1"
};

It could be written in such a way as to bring about additional space compression, but UglifyJS does not do this

let v1_1 = "1.1",//This variable will make the compressed code 7 bytes smaller!
//Initialize all sstp operations
sstp_version_table = {
	SEND: "1.4",
	NOTIFY: v1_1,
	COMMUNICATE: v1_1,
	EXECUTE: "1.2",
	GIVE: v1_1
};
//let _="1.1",v={SEND:"1.4",NOTIFY:_,COMMUNICATE:_,EXECUTE:"1.2",GIVE:_}

As long as it's not global, we can compress things that are used repeatedly in a piece of code and are long enough to be compressed further by letting some variables At the moment I'm manually bringing things up in the files I maintain and putting them at the beginning of the block to compress the size of the resulting min.js file, but I think doing this manually is little silly, breaks the readability of the code and makes maintenance a lot harder https://github.com/ukatech/jsstp-lib/blob/32fa164cbcac736d48b536a6487ee3d7454d410b/src/base.mjs#L2L15 I'm hoping that UglifyJS will do this for me.

These are some examples of this issue


method1("long str");
method2(x,"long str");

=>

let a="long str";
method1(a);
method2(x,a);

mytool.long_method_name();
//...
anothertool.long_method_name();

=>

let a="long_method_name";
mytool[a]();
//...
anothertool[a]();

a?b:undefined;

=>

let u;
a?b:u;

steve02081504 avatar Jun 10 '23 07:06 steve02081504

@alexlamsl Can you take a look at this? I'm not sure if this has a performance impact, but I think it would reduce the source code size significantly

steve02081504 avatar Jun 18 '23 08:06 steve02081504

Optimisations like this (and their pitfall) are discussed under #75 and related issues.

alexlamsl avatar Jun 19 '24 07:06 alexlamsl