Q:

To serialize directly to file stream or buffer before

Here I was wondering what is generally considered to be faster.
Either writing to the stream directly while serializing data
using (var fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None))
{
    TypeSerializer.SerializeToStream(data, fs);
}

vs. buffering the serialized data before and writing everything at once to the stream
string buffer = TypeSerializer.SerializeToString(data);
using (StreamWriter sw = new StreamWriter(file, false))
{
    sw.Write(buffer);
}

My example here uses ServiceStack.Text as I am especially interested in what its developer thinks about it but I guess this applies also to all other serializing methods.
Also I know that FileStream uses an internal buffer but I am not too sure if I can trust it when for example serializing, for whatever unlikely reason, is slower than disk IO  :)

A:

It really depends.
If you are talking about a lot of data, then loading it to memory will cost. even cost much (not to mention how much the GC will have to work to get rid of this temporary data) - which is a waste.
On server side application, loading all the data into memory and writing all at once will be a big throughput no-no. It could be the difference between a well-performing application, to a non-working at all application, when the server is being stressed.
As a general idea, Streaming is the way to go.
In terms of single call, it really depends on the size of the data, the buffers, IO latency, etc. Need to do your testing to find the magical numbers (chunk sizes).
Regarding the flush policy (internal buffer for writes), you can always make sure it persisted by calling FlushMethod on the stream itself, or on the wrapping StreamWriter.
Hope this helps, 
Ofir.

