According to the measurements, replaceRange
+ append
works faster than setText
except for the case when text is fully replaced.
In 100% replace case setText
is on par with replaceRange
+ append
(the results are within error bounds).
For small appends there's a win in terms of response time and memory allocation.
# JMH 1.12 (released 309 days ago, please consider updating!)
# VM version: JDK 1.8.0_102, VM 25.102-b14
# VM invoker: /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/bin/java
Benchmark (maxLength) Mode Cnt Score Error Units
TextAreaBenchmark.append100pct 100000 avgt 5 0,558 ± 0,142 ms/op
TextAreaBenchmark.append100pct:·gc.alloc.rate.norm 100000 avgt 5 1644296,258 ± 0,067 B/op
TextAreaBenchmark.append50pct 100000 avgt 5 0,466 ± 0,070 ms/op
TextAreaBenchmark.append50pct:·gc.alloc.rate.norm 100000 avgt 5 922424,215 ± 0,032 B/op
TextAreaBenchmark.append_1line 100000 avgt 5 0,394 ± 0,031 ms/op
TextAreaBenchmark.append_1line:·gc.alloc.rate.norm 100000 avgt 5 201824,182 ± 0,014 B/op
TextAreaBenchmark.append_2lines 100000 avgt 5 0,374 ± 0,019 ms/op
TextAreaBenchmark.append_2lines:·gc.alloc.rate.norm 100000 avgt 5 203912,173 ± 0,009 B/op
TextAreaBenchmark.baseline_getLog 100000 avgt 5 0,101 ± 0,014 ms/op
TextAreaBenchmark.baseline_getLog:·gc.alloc.rate.norm 100000 avgt 5 777672,047 ± 0,007 B/op
TextAreaBenchmark.setText 100000 avgt 5 0,539 ± 0,046 ms/op
TextAreaBenchmark.setText:·gc.alloc.rate.norm 100000 avgt 5 1578840,249 ± 0,021 B/op