diff --git a/map.go b/map.go index 7d53b94e..27b00752 100644 --- a/map.go +++ b/map.go @@ -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] { @@ -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] {