From d9d3e3fb05150de6b22fc580bc458e9e6feffc35 Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Sun, 11 Aug 2024 18:28:42 +0200 Subject: [PATCH] feat: preallocate output for lo.Keys and lo.Values --- map.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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] {