Performance Tip: Avoid using a String when char will do.

Overview

There are a number of operations where you can use a String or a char. In each case the char option is likely to be fastest.

Use String.indexOf with a char instead of a String

From URI.appendAuthority()
int end = authority.indexOf("]");
if (end != -1 && authority.indexOf(":")!=-1) {
In the same method, indexOf(':') is used.

Appending a char instead of a String

Using a char instead of a String can make a small difference.

From File.slashify()
if (!p.startsWith("/"))
   p = "/" + p;
if (!p.endsWith("/") && isDirectory())
   p = p + "/";
In both case a '/' could be used instead of a '/'
   p = p + '/';

Comments

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues