Go Benchmark
Time Format vs AppendFormat
Formatting timestamps with a fresh string vs appending into a reused buffer.
Formatting timestamps is a hidden hot path in most services — every log line, metric, and API response carries one. time.Time.Format returns a freshly allocated string on every call, while AppendFormat writes into a caller-provided byte slice, allowing the buffer to be reused across calls. This benchmark formats the same time as RFC 3339 with both APIs. The formatting work is identical; the difference is one heap allocation per call — which is exactly why loggers like zap and zerolog are built on the Append* style APIs. The same pattern applies to strconv.AppendInt vs strconv.Itoa and friends.
1 CPU
32 CPUs
t.AppendFormat(buf[:0], time.RFC3339) with a buffer reused across iterations. Identical formatting logic, but the output lands in memory you already own: zero allocations per call once the buffer exists. The natural fit for loggers, encoders, and any code that writes the bytes onward instead of storing a string.
t.Format(time.RFC3339) — the everyday API. Internally it calls AppendFormat into a temporary buffer and then converts the result to a string, which is where the per-call heap allocation comes from. Perfectly fine anywhere that isn't hot.
Contributors