Skip to content

Commit

Permalink
optimize slackutilsx.EscapeMessage function
Browse files Browse the repository at this point in the history
  • Loading branch information
brainexe committed Mar 9, 2022
1 parent f6658aa commit 60ea6a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions slackutilsx/slackutilsx.go
Expand Up @@ -50,10 +50,12 @@ func DetectChannelType(channelID string) ChannelType {
}
}

// initialize replacer only once (if needed)
var escapeReplacer = strings.NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;")

// EscapeMessage text
func EscapeMessage(message string) string {
replacer := strings.NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;")
return replacer.Replace(message)
return escapeReplacer.Replace(message)
}

// Retryable errors return true.
Expand Down
6 changes: 6 additions & 0 deletions slackutilsx/slackutilsx_test.go
Expand Up @@ -28,3 +28,9 @@ func TestEscapeMessage(t *testing.T) {
test("A < B", "A &lt; B")
test("A > B", "A &gt; B")
}

func BenchmarkEscapeMessage(b *testing.B) {
for i := 0; i < b.N; i++ {
EscapeMessage("A & B")
}
}

0 comments on commit 60ea6a8

Please sign in to comment.