I was surprised to see that initializing an empty std::string to an empty string literal "" was faster than using the default constructor {}. The generated assembly code shows that return ""; only zeroes 2 bytes, while return {}; zeroes 24 bytes. Why is return ""; so much better?
The reason return ""; is faster than return {}; is because of a concept called small string optimization (SSO).
In the case of return "";, an empty string literal "" is assigned to the std::string object. Since the string literal is known at compile-time and has a size of 1 byte (for the null terminator), the std::string object can take advantage of SSO. SSO allows small strings to be stored directly in the std::string object itself, without dynamically allocating memory on the heap. In this case, the std::string object doesn’t need to allocate any memory and can simply store the null terminator.
On the other hand, return {}; uses the default constructor of std::string, which initializes the object with a null pointer and a size of 0. This requires allocating memory on the heap to store the empty string. In this case, the std::string object needs to allocate memory, set the size to 0, and store the null pointer.
As a result, return ""; only needs to zero out 2 bytes (the null terminator), while return {}; needs to zero out 24 bytes (to set the null pointer and size to 0).
Therefore, return ""; is faster because it takes advantage of SSO and avoids the overhead of dynamic memory allocation.