Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Jan 7, 2024
1 parent d71ce36 commit f90257e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ goos: darwin
goarch: arm64
pkg: resenje.org/casbab
BenchmarkCamel
BenchmarkCamel-10 1826664 642.2 ns/op 640 B/op 9 allocs/op
BenchmarkCamel-10 2106188 569.1 ns/op 640 B/op 9 allocs/op
BenchmarkPascal
BenchmarkPascal-10 1830391 638.6 ns/op 640 B/op 9 allocs/op
BenchmarkPascal-10 2123118 567.5 ns/op 640 B/op 9 allocs/op
BenchmarkSnake
BenchmarkSnake-10 1868508 653.8 ns/op 640 B/op 9 allocs/op
BenchmarkSnake-10 2091612 567.1 ns/op 640 B/op 9 allocs/op
BenchmarkCamelSnake
BenchmarkCamelSnake-10 1782997 685.2 ns/op 640 B/op 9 allocs/op
BenchmarkCamelSnake-10 2032400 586.8 ns/op 640 B/op 9 allocs/op
BenchmarkScreamingSnake
BenchmarkScreamingSnake-10 1583528 740.1 ns/op 640 B/op 9 allocs/op
BenchmarkScreamingSnake-10 1753968 637.1 ns/op 640 B/op 9 allocs/op
BenchmarkKebab
BenchmarkKebab-10 1859630 640.6 ns/op 640 B/op 9 allocs/op
BenchmarkKebab-10 2113995 571.8 ns/op 640 B/op 9 allocs/op
BenchmarkCamelKebab
BenchmarkCamelKebab-10 1825042 658.5 ns/op 640 B/op 9 allocs/op
BenchmarkCamelKebab-10 2047392 586.0 ns/op 640 B/op 9 allocs/op
BenchmarkScreamingKebab
BenchmarkScreamingKebab-10 1685986 713.3 ns/op 640 B/op 9 allocs/op
BenchmarkScreamingKebab-10 1863102 635.0 ns/op 640 B/op 9 allocs/op
BenchmarkLower
BenchmarkLower-10 1862577 640.1 ns/op 640 B/op 9 allocs/op
BenchmarkLower-10 2107701 566.4 ns/op 640 B/op 9 allocs/op
BenchmarkTitle
BenchmarkTitle-10 1818093 657.6 ns/op 640 B/op 9 allocs/op
BenchmarkTitle-10 2056044 584.0 ns/op 640 B/op 9 allocs/op
BenchmarkScreaming
BenchmarkScreaming-10 1647198 725.4 ns/op 640 B/op 9 allocs/op
BenchmarkScreaming-10 1887225 644.4 ns/op 640 B/op 9 allocs/op
PASS
```
37 changes: 25 additions & 12 deletions casbab.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,10 @@ func scream(s [][]rune) [][]rune {

func camel(s [][]rune, start int) [][]rune {
for i := start; i < len(s); i++ {
r := []rune(s[i])
if len(r) == 0 {
if len(s[i]) == 0 {
continue
}
s[i][0] = unicode.ToUpper(r[0])
s[i][0] = unicode.ToUpper(s[i][0])
}
return s
}
Expand Down Expand Up @@ -260,9 +259,14 @@ func join(elems [][]rune) string {
}

var b strings.Builder
b.WriteString(string(elems[0]))

for _, e := range elems[0] {
b.WriteRune(e)
}
for _, s := range elems[1:] {
b.WriteString(string(s))
for _, e := range s {
b.WriteRune(e)
}
}
return b.String()
}
Expand All @@ -276,15 +280,20 @@ func joinSep(elems [][]rune, sep rune) string {
}

var b strings.Builder
b.WriteString(string(elems[0]))

for _, e := range elems[0] {
b.WriteRune(e)
}
for _, s := range elems[1:] {
b.WriteRune(sep)
b.WriteString(string(s))
for _, e := range s {
b.WriteRune(e)
}
}
return b.String()
}

func joinWrap(elems [][]rune, sep rune, prefixSize, suffixSize int) string {
func joinWrap(elems [][]rune, sep rune, head, tail int) string {
switch len(elems) {
case 0:
return ""
Expand All @@ -294,17 +303,21 @@ func joinWrap(elems [][]rune, sep rune, prefixSize, suffixSize int) string {

var b strings.Builder

for i := 0; i < prefixSize; i++ {
for i := 0; i < head; i++ {
b.WriteRune(sep)
}

b.WriteString(string(elems[0]))
for _, e := range elems[0] {
b.WriteRune(e)
}
for _, s := range elems[1:] {
b.WriteRune(sep)
b.WriteString(string(s))
for _, e := range s {
b.WriteRune(e)
}
}

for i := 0; i < suffixSize; i++ {
for i := 0; i < tail; i++ {
b.WriteRune(sep)
}
return b.String()
Expand Down

0 comments on commit f90257e

Please sign in to comment.