If performance is a concern, especially in cases where you need a lot of case-insensitive key lookups, you should check out [cimap
] (https://github.com/projectbarks/cimap). [1] The issue with using strings.ToLower
on every key lookup is that it causes unnecessary allocations, which can slow things down significantly.
cimap
avoids this by converting keys inline without creating extra strings, leading to 0 B/op and 0 allocs/op for common operations like Add
, Get
, and Delete
. In benchmarks, it shows a 50%+ speed improvement over traditional case-insensitive maps.
Here’s a quick example:
m := cimap.New[string]()
m.Add("Hello", "world")
fmt.Println(m.Get("hello")) // "world"
fmt.Println(m.Get("HELLO")) // "world"
If you’re dealing with a lot of string keys and care about performance, cimap
is a much better option than manually lowercasing keys all the time.
[1] Disclosure: I am the original author of the cimap
library. The code is released under the MIT License, which means you are free to copy, modify, and redistribute. I want to emphasize that I do not profit from this library in any way; my goal is simply to contribute to the open-source community.