Skip to content

SONARJAVA-5580 modify S3024: add performance benchmark table #5082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 27, 2025
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions rules/S3024/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The use of a ``++StringBuilder++`` or ``++StringBuffer++`` is supposed to make `

=== Noncompliant code example

[source,java]
[source,java,diff-id=1,diff-type=noncompliant]
----
StringBuilder sb = new StringBuilder();
sb.append("foo is: " + getFoo()); // Noncompliant
Expand All @@ -14,12 +14,46 @@ sb.append("foo is: " + getFoo()); // Noncompliant

=== Compliant solution

[source,java]
[source,java,diff-id=2,diff-type=compliant]
----
StringBuilder sb = new StringBuilder();
sb.append("foo is: ").append(getFoo());
----

== Resources

=== Benchmarks

[options="header"]
|===
| method| Runtime| Average time| Error margin
| append| Temurin 21| 26.3 ns/op| ±0.43 ns/op
| concat| Temurin 21| 30.3 ns/op| ±0.81 ns/op
|===

The results were generated by running the following snippet with https://github.com/openjdk/jmh[jmh]:

[source,java]
----
private String name1 = "John";
private String name2 = "Jane";

@Benchmark
public String concat() {
StringBuilder sb = new StringBuilder();
sb.append(name1 + ", " + name2);
return sb.toString();
}

@Benchmark
public String append() {
StringBuilder sb = new StringBuilder();
sb.append(name1).append(", ").append(name2);
return sb.toString();
}
----


ifdef::env-github,rspecator-view[]

'''
Expand Down