Skip to content

Commit

Permalink
feat: preallocate output for lo.Keys and lo.Values
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Aug 11, 2024
1 parent 1603a84 commit d9d3e3f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package lo
// Keys creates an array of the map keys.
// Play: https://go.dev/play/p/Uu11fHASqrU
func Keys[K comparable, V any](in ...map[K]V) []K {
result := make([]K, 0)
size := 0
for i := range in {
size += len(in[i])
}
result := make([]K, 0, size)

for i := range in {
for k := range in[i] {
Expand Down Expand Up @@ -43,7 +47,11 @@ func HasKey[K comparable, V any](in map[K]V, key K) bool {
// Values creates an array of the map values.
// Play: https://go.dev/play/p/nnRTQkzQfF6
func Values[K comparable, V any](in ...map[K]V) []V {
result := make([]V, 0, len(in))
size := 0
for i := range in {
size += len(in[i])
}
result := make([]V, 0, size)

for i := range in {
for k := range in[i] {
Expand Down

0 comments on commit d9d3e3f

Please sign in to comment.