Looking for the best way to protect your JavaScript source code? Stop looking. Sign up now! Did you forget your password?

JScrambler JScrambler

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

JavaScript comments as // or /* */ are removed from your JavaScript. They are not needed by the Web browser to run your code.

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

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.

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);
      }
   };
}

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.

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

Private declarations inside the same scope will share the same reserved word var instead of each one having their own 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

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.

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

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.

source code:

f1(false,false,false);
f3(false,false);

minified code:

var a = false;
f1(a,a,a);
f3(a,a);

Name reusing

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.

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;
...
}

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.

Back ↵