5

Is there any significant performance difference between the following two?

String json = mapper.writeValueAsString(searchResult);
response.getWriter().write(json);

vs

mapper.writeValue(response.getWriter(), searchResult);
Sagar
  • 5,315
  • 6
  • 37
  • 66

1 Answers1

1

writeValueAsString JavaDoc says:

Method that can be used to serialize any Java value as a String. Functionally equivalent to calling writeValue(Writer,Object) with StringWriter and constructing String, but more efficient.

So, in case, you want to write JSON to String is much better to use this method than writeValue. Both these methods use _configAndWriteValue.

In your case it is better to write JSON directly to response.getWriter() than generating String object and after that writing it to response.getWriter().

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146