JavaScript Minification Examples
JScrambler offers several advanced JavaScript minification transformations. These can be combined to achieve the best possible JavaScript compression.
- Source code
-
// ---------------------------------------------------
// Simple object definition of financial independence
// ---------------------------------------------------
function FinancialIndependence(mDeposit, wAnnualInterest, dAmount)
{
var self = this;
var yearsCount = 0;
var pf = parseFloat;
self.monthlyDeposit = pf(mDeposit);
self.withAnnualInterest = pf(wAnnualInterest);
self.desiredAmount = pf(dAmount);
/*
* Method to calculate the years needed to reach a desired ammount
*/
self.calculateYearsForAmount = function(have)
{
var alreadySaved = have + self.monthlyDeposit * 12;
var afterInterest = alreadySaved + alreadySaved * self.withAnnualInterest;
if(afterInterest >= self.desiredAmount)
{
return yearsCount;
}
else
{
yearsCount++;
return self.calculateYearsForAmount(afterInterest);
}
};
} - Remove code comments
-
function FinancialIndependence(mDeposit, wAnnualInterest, dAmount)
{
var self = this;
var yearsCount = 0;
var pf = parseFloat;
self.monthlyDeposit = pf(mDeposit);
self.withAnnualInterest = pf(wAnnualInterest);
self.desiredAmount = pf(dAmount);
self.calculateYearsForAmount = function(have)
{
var alreadySaved = have + self.monthlyDeposit * 12;
var afterInterest = alreadySaved + alreadySaved * self.withAnnualInterest;
if(afterInterest >= self.desiredAmount)
{
return yearsCount;
}
else
{
yearsCount++;
return self.calculateYearsForAmount(afterInterest);
}
};
} - Replace local names for meaningless smaller ones
-
function FinancialIndependence(f,g,h)
{
var a = this;
var d = 0;
var i = parseFloat;
a.monthlyDeposit = i(f);
a.withAnnualInterest = i(g);
a.desiredAmount = i(h);
a.calculateYearsForAmount = function(e)
{
var b = e + a.monthlyDeposit * 12;
var c = b + b * a.withAnnualInterest;
if(c >= a.desiredAmount)
{
return d;
}
else
{
d++;
return a.calculateYearsForAmount(c);
}
};
} - Remove unnecessary braces and semicolons
-
function FinancialIndependence(f,g,h)
{
var a = this;
var d = 0;
var i = parseFloat;
a.monthlyDeposit = i(f);
a.withAnnualInterest = i(g);
a.desiredAmount = i(h);
a.calculateYearsForAmount = function(e)
{
var b = e + a.monthlyDeposit * 12;
var c = b + b * a.withAnnualInterest;
if(c >= a.desiredAmount)
return d;
else
{
d++;
return a.calculateYearsForAmount(c)
}
}
} - Share the same var with more than one declaration
-
function FinancialIndependence(f,g,h)
{
var a = this,
d = 0,
i = parseFloat;
a.monthlyDeposit = i(f);
a.withAnnualInterest = i(g);
a.desiredAmount = i(h);
a.calculateYearsForAmount = function(e)
{
var b = e + a.monthlyDeposit * 12,
c = b + b * a.withAnnualInterest;
if(c >= a.desiredAmount)
return d;
else
{
d++;
return a.calculateYearsForAmount(c)
}
}
} - Remove unnecessary whitespaces and newlines
-
function FinancialIndependence(f,g,h){var a=this,d=0,i=parseFloat;a.monthlyDeposit=i(f);a.withAnnualInterest=i(g);a.desiredAmount=i(h);a.calculateYearsForAmount=function(e){var b=e+a.monthlyDeposit*12,c=b+b*a.withAnnualInterest;if(c>=a.desiredAmount)return d;else{d++;return a.calculateYearsForAmount(c)}}}
- Create declarations to replace literal duplicates
-
source code:
f1(false,false,false);
f3(false,false);
-
minified code:
var a = false;
f1(a,a,a);
f3(a,a);
- Name reusing
-
source code:
function f1(arg1, arg2, arg3)
{
...
}
function f2()
{
var var1, var2, var3;
...
}
-
minified code:
function f1(a, b, c)
{
...
}
function f2()
{
var a, b, c;
...
}
JavaScript comments as // or /* */ are removed from your JavaScript. They are not needed by the Web browser to run your code.
Since local names are only accessible inside the scope where they have been declared (and inner scopes), they can be replaced without worrying about other dependencies. The same is not true for public or protected names.
You can chose to replace all identifiers using JScrambler. To maintain external source dependencies you can define a list of exceptions. JScrambler automatically preserves reserved words, HTML DOM and browser elements.
Private declarations inside the same scope will share the same reserved word var instead of each one having their own declaration.
There are some whitespaces that can not be removed without damaging the source code and others that can. All those that can be removed, will be.
This only happens when replacing is worth it - meaning that if the replacing would add more characters that the existing ones, the transformation does not occur.
JScrambler replaces unnecessarily big and verbose local names, with smaller ones. The new names are reused whenever it is possible. JScrambler takes into account the element scope to avoid quick name size growth.
When combined, these transformations can achieve an high level of JavaScript compression (minification). You can see some real world examples here. Sign up now to compress and minify your JavaScript.