Home > Education > Top 10 Easy Performance Optimisations in Java

Top 10 Easy Performance Optimisations in Java

Added: (Mon Jan 29 2018)

Pressbox (Press Release) - In this article, we’ve talked about optimisations done on the N.O.P.E. branch, i.e. deep down in a high-complexity algorithm. In our case, being the jOOQ developers, we have interest in optimising our SQL generation:
1. Use StringBuilder
This should be your default in almost all Java code. Try to avoid the + operator.
may argue that it is just syntax sugar for a StringBuilder anyway, as in:
1 String x = "a" + args.length + "b";
But what happens, if later on, you need to amend your String with optional parts?
4 String x = "a" + args.length + "b";

if (args.length == 1)
x = x + args[0];
You will now have a second StringBuilder, that just needlessly consumes memory off your heap, putting pressure on your GC. Write this instead:
6 StringBuilder x = new StringBuilder("a");

if (args.length == 1);
2. Avoid regular expressions
Regular expressions are relatively cheap and convenient. But if you’re in the N.O.P.E. branch, they’re about the worst thing you can do. If you absolutely must use regular expressions in computation-intensive code sections, at least cache the Pattern reference instead of compiling it afresh all the time:
3. Do not use iterator()
Now, this advice is really not for general use-cases, but only applicable deep down in a N.O.P.E. branch. Nonetheless, you should think about it. Writing Java-5 style foreach loops is convenient. You can just completely forget about looping internals, and write:
3 for (String value : strings) {
// Do something useful here
However, every time you run into this loop, if strings is an Iterable, you will create a new Iterator instance. If you’re using an ArrayList, this is going to be allocating an object with 3 ints on your heap:

4. Don’t call that method
Some methods are simple expensive. In our N.O.P.E. branch example, we don’t have such a method at the leaf, but you may well have one. Let’s assume your JDBC driver needs to go through incredible trouble to calculate the value of ResultSet.wasNull(). Your homegrown SQL framework code might look like this:
10 if (type == Integer.class) {
result = (T) wasNull(rs,

// And then...
static final T wasNull(ResultSet rs, T value)
throws SQLException {
return rs.wasNull() ? null : value;
This logic will now call ResultSet.wasNull() every time you get an int from the result set. But the getInt() contract reads:
5. Use primitives and the stack
The above example is from jOOQ, which uses a lot of generics, and thus is forced to use wrapper types for byte, short, int, and long – at least before generics will be specialisable in Java 10 and project Valhalla. But you may not have this constraint in your code, so you should take all measures to replace:
6. Avoid recursion
Modern functional programming languages like Scala encourage the use of recursion, as they offer means of optimising tail-recursing algorithms back into iterative ones. If your language supports such optimisations, you might be fine. But even then, the slightest change of algorithm might produce a branch that prevents your recursion from being tail-recursive. Hopefully the compiler will detect this! Otherwise, you might be wasting a lot of stack frames for something that might have been implemented using only a few local variables.
7. Use entrySet()
When you want to iterate through a Map, and you need both keys and values, you must have a very good reason to write the following:
8. Use EnumSet or EnumMap
There are some cases where the number of possible keys in a map is known in advance – for instance when using a configuration map. If that number is relatively small, you should really consider using EnumSet or EnumMap, instead of regular HashSet or HashMap instead. This is easily explained by looking at EnumMap.put():
9. Optimise your hashCode() and equals() methods
If you cannot use an EnumMap, at least optimise your hashCode() and equals() methods. A good hashCode() method is essential because it will prevent further calls to the much more expensive equals() as it will produce more distinct hash buckets per set of instances.
10. Think in sets, not in individual elements
Last but not least, there is a thing that is not Java-related but applies to any language. Besides, we’re leaving the N.O.P.E. branch as this advice might just help you move from O(N3) to O(n log n), or something like that.
Unfortunately, many programmers think in terms of simple, local algorithms. They’re solving a problem step by step, branch by branch, loop by loop, method by method. That’s the imperative and/or functional programming style. While it is increasingly easy to model the “bigger picture” when going from pure imperative to object oriented (still imperative) to functional programming, all these styles lack something that only SQL and R and similar languages have:
• Every query is generated only on a single StringBuilder
• Our templating engine actually parses characters, instead of using regular expressions
• We use arrays wherever we can, especially when iterating over listeners
• We stay clear of JDBC methods that we don’t have to call
• etc…
jOOQ is at the “bottom of the food chain”, because it’s the (second-)last API that is being called by our customers’ applications before the call leaves the JVM to enter the DBMS. Being at the bottom of the food chain means that every line of code that is executed in jOOQ might be called N x O x P times, so we must optimise eagerly.

Submitted by:infocampus
Disclaimer: Pressbox disclaims any inaccuracies in the content contained in these releases. If you would like a release removed please send an email to remove@pressbox.co.uk together with the url of the release.