Skip to content

Instantly share code, notes, and snippets.

@delanym
Created October 14, 2025 07:27
Show Gist options
  • Select an option

  • Save delanym/c6bd8adcf9fe6950b40f04786d4c1b74 to your computer and use it in GitHub Desktop.

Select an option

Save delanym/c6bd8adcf9fe6950b40f04786d4c1b74 to your computer and use it in GitHub Desktop.
Optional benchmark
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
@Fork(1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 100, timeUnit = TimeUnit.MILLISECONDS)
public class OptionalBenchmark {
private static boolean returnBoolean() {
return true;
}
// Plain get a boolean
@Benchmark
public void getBoolean(Blackhole bh) {
bh.consume(returnBoolean());
}
private static boolean throwException() {
throw new NullPointerException();
}
@Benchmark
public void getThrow(Blackhole bh) {
try {
bh.consume(throwException());
} catch (NullPointerException ignored) {
}
}
// BOOLEAN OPTIONAL
private Optional<Boolean> returnOptionalBoolean() {
return Optional.of(true);
}
// Get an Optional and do nothing
@Benchmark
public void getOptionalBoolean(Blackhole bh) {
bh.consume(returnOptionalBoolean());
}
// Get an Optional and get it
@Benchmark
public void getOptionalBooleanGet(Blackhole bh) {
bh.consume(returnOptionalBoolean().get());
}
// Get an Optional by looping over it
@Benchmark
public void getOptionalBooleanFor(Blackhole bh) {
returnOptionalBoolean().stream().toList().forEach(bh::consume);
}
// Get an Optional, check it, then get it
@Benchmark
public void getOptionalBooleanIsPresentGet(Blackhole bh) {
bh.consume(returnOptionalBoolean().isPresent());
bh.consume(returnOptionalBoolean().get());
}
// Get an Optional, check it, then get it, or else throw
@Benchmark
public void getOptionalBooleanOrThrow(Blackhole bh) {
try {
bh.consume(returnOptionalBoolean().orElseThrow(NullPointerException::new));
} catch (NullPointerException ignored) {
}
}
// EMPTY OPTIONAL
private Optional<Boolean> returnOptionalEmpty() {
return Optional.empty();
}
// Get an empty Optional and do nothing
@Benchmark
public void getOptionalEmpty(Blackhole bh) {
bh.consume(returnOptionalEmpty());
}
// Get an empty Optional and get it
@Benchmark
public void getOptionalEmptyGet(Blackhole bh) {
try {
bh.consume(returnOptionalEmpty().get());
} catch (NoSuchElementException ignored) {
}
}
// Get an empty Optional by looping over it
@Benchmark
public void getOptionalEmptyFor(Blackhole bh) {
try {
returnOptionalEmpty().stream().toList().forEach(bh::consume);
} catch (NoSuchElementException ignored) {
}
}
// Get an empty Optional, check it, then get it
@Benchmark
public void getOptionalEmptyIsPresentGet(Blackhole bh) {
if (returnOptionalEmpty().isPresent())
bh.consume(returnOptionalEmpty().get());
}
// Get an empty Optional, check it, then get it, or else throw
@Benchmark
public void getOptionalEmptyOrThrow(Blackhole bh) {
try {
bh.consume(returnOptionalEmpty().orElseThrow(NullPointerException::new));
} catch (NullPointerException ignored) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment