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

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

Open
steve02081504 opened this issue Jun 10, 2023 · 1 comment

Comments

@steve02081504
Copy link

steve02081504 commented Jun 10, 2023

(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
Copy link
Author

@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

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

1 participant