diff --git a/benchmark_test.go b/benchmark_test.go index 6918e9d..9d0ee5c 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -166,3 +166,18 @@ func BenchmarkToFormattedCase_Default(b *testing.B) { _ = ToFormattedCase(words) } } + +func BenchmarkSplitMixCase(b *testing.B) { + words := []Word{ + ExactCaseWord("thisIsAMixedCaseString"), + ExactCaseWord("AnotherMixedCaseStringWithMoreParts"), + ExactCaseWord("ShortOne"), + ExactCaseWord("SuperLongMixedCaseStringWithManyManyCapitalLettersToTriggerReallocationIfBufferIsTooSmall"), + } + + b.ResetTimer() + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _, _ = WordsToFormattedCase(words, OptionMixCaseSupport(), OptionDelimiter("-")) + } +} diff --git a/types.go b/types.go index 832cb05..a1c2d41 100644 --- a/types.go +++ b/types.go @@ -354,8 +354,13 @@ func separateOptionsAny(opts []any) ([]any, []any) { // Helper function to split words in mixed case func splitMixCase(input, delimiter string) string { + if delimiter == "" { + return input + } var result strings.Builder - result.Grow(len(input)) + // Pre-allocate to avoid resizing. + // We add a buffer for potential delimiters (assuming roughly 50% increase). + result.Grow(len(input) + len(input)/2) for i, r := range input { if i > 0 && unicode.IsUpper(r) { result.WriteString(delimiter)