Tags

, , ,

On Tips Performance series, I’ll give tips of little or almost little things that can be done to improve application performance.

Optimize the FOR loop

Reason: Every time FOR ends an iteration, it validates the condition to know whether to continue. In this condition, it calls a method of the list, thus, the list size will be calculated for each iteration.

Explanation: By placing the value of the list size in a variable, access is much faster because it is not necessary to calculate in all iterations, because it uses the variable that already contains the reference in memory.

Before:

for (int i = 0; i <= list.size(); i++) { ... }

After:

for (int i = 0, j = list.size(); i <= j; i++) { ... }

Programming on the Right Scopes

Reason: Many objects in broad scopes are unused most of the time. Much of the programming languages currently have a garbage collection, cleaning objects that are no longer being used / referenced.

Explanation: Programming on the right scopes makes the object no longer referenced earlier because it only exists for that scope. Thus, once the garbage collector does the cleaning, will be more effective.

Before:

public class SomeClass {
  private MyClass myClass = new MyClass();

  public void useMyClass() {
    //... (using myClass);
  }
}

After:

public class SomeClass {

  public void useMyClass() {
    MyClass myClass = new MyClass();
    //... (using myClass);
  }
}

Minify JS and CSS

Reason: Each file JS and CSS mean a call to the server, and they are usually Blocking: while a file is not downloaded, they might do not continue other calls. This means that if there is instability of a single file, others will expect the completion of this file.

Explanation: Minify JS and CSS not only reduces the file size as, once you minify several JS in one and do the same with CSS, you would have only two calls to the server for these static files.

Before:

<link rel="stylesheet" type="text/css" href="/myCSS01.css"/>
<link rel="stylesheet" type="text/css" href="/myCSS02.css"/>
<link rel="stylesheet" type="text/css" href="/myCSS03.css"/>
<script type="text/javascript" src="/myJS01.js"></script>
<script type="text/javascript" src="/myJS02.js"></script>
<script type="text/javascript" src="/myJS03.js"></script>

After:

<link rel="stylesheet" type="text/css" href="/myCSS.min.css"/>
<script type="text/javascript" src="/myJS.min.js"></script>

More about Minify: http://minifycode.com/

Reuse Exception

Reason: Exceptions typically cost a lot of memory, but most of the cost is in creation (instantiation) of Exception, and many exceptions hasn’t dynamic responses.

Explanation: By creating a pool of Exception, if used with a singleton, it does not become costly always throw an exception, since the cost of creation would be run once. It is noteworthy that is only recommended for Exceptions without dynamic messages.

P.S.: This is just one of the possible implementations of this idea.

Before:

try { 
   ...
   throw new BadRequestException("Always the same message");
} ...

After:

try { 
 ...
 throw ExceptionPool.getBadRequestException();
} ...

//ExceptionPool
public abstract class ExceptionPool {
    private static BadRequestException bre;
    public static BadRequestException getBadRequestException() {
       if(bre == null) {
          bre = new BadRequestException("Always the same message");
       }
       return bre;
    }
}

Concat with StringBuilder

Reason: Depending on how the code was created, the Java compiler can translate the code in a way that has not a good performance.

Explanation: By using the StringBuilder, you ensure that no additional object is created unnecessarily by the compiler for the concatenation.

Before – How the Java compiler translates:

//This code:
String answer = integerPart;
answer += ".";
answer += mantissa;

//Translates into:
String answer = new StringBuilder(integer).toString();
answer = new StringBuilder(answer).append(".").toString();
answer = new StringBuilder(answer).append(mantissa).toString();

After:

StringBuilder builder = new StringBuilder();
builder.append(integerPart); //you can do one by one
builder.append(".").append(mantissa); //or you can continue
String answer = builder.toString();

References: Java Perfomance: The Definitive Guide (Scott Oaks); Java Performance Tuning (Jack Shirazi)

Advertisements