Skip to content

Instantly share code, notes, and snippets.

@garlicnation
Created May 22, 2015 07:20
Show Gist options
  • Save garlicnation/f1ba9726da86c75d6918 to your computer and use it in GitHub Desktop.
Save garlicnation/f1ba9726da86c75d6918 to your computer and use it in GitHub Desktop.

Why minification will always help compression:

Take:

function (apples) {
	var all_my_apples = ["another apple"];
	for (var i = 0; i < apples.length; i++) {
		all_my_apples.push(apples[i]);
	}
	return all_my_apples;
}

function (oranges) {
	var some_of_my_oranges = ["another orange"];
	for (var i = 0; i < oranges.length; i++) {
		some_of_my_oranges.push(oranges[i]);
	}
	return some_of_my_oranges;
}

The total entropy in this code is reduced significantly by having different variable names. By trading in readability, the available information in the code is being reduced. Even without reducing the size of the application at all, a hypothetical JS optimizing tool could safely transform the above into.

function (apples) {
	var all_my_apples = ["another apple"];
	for (var i = 0; i < apples.length; i++) {
		all_my_apples.push(apples[i]);
	}
	return all_my_apples;
}

function (apples) {
	var all_my_apples = ["another orange"];
	for (var i = 0; i < apples.length; i++) {
		all_my_apples.push(apples[i]);
	}
	return all_my_apples;
}

Now, there is significantly more repetition for an optimal compression to pick up on, all_my_apples is repeated 6 times, and every other part of the function definition is duplicated.

In essense, minification is lossy compression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment