Skip to content

Instantly share code, notes, and snippets.

@judepereira
Created September 4, 2023 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judepereira/f67c2a8840a45440d39665e5160126f8 to your computer and use it in GitHub Desktop.
Save judepereira/f67c2a8840a45440d39665e5160126f8 to your computer and use it in GitHub Desktop.
private static final ObjectMapper OM = new ObjectMapper();
private static final String STR = "hello world - the quick brown fox jumps over "
+ "the lazy dog\r\n\r\nand here's "
+ "a random slash\\, and some \"s";
@Benchmark
public void jsonStringSerialization(final Blackhole blackhole) throws Exception {
byte[] obj = OM.writeValueAsBytes(STR);
blackhole.consume(new String(obj, 1, obj.length - 2, StandardCharsets.UTF_8));
}
@Benchmark
public void jsonStringManual(final Blackhole blackhole) {
String str = STR;
if (str.contains("\\")) {
str = str.replace("\\", "\\\\");
}
if (str.contains("\"")) {
str = str.replace("\"", "\\\"");
}
blackhole.consume(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment