diff --git a/filter.go b/filter.go index cc1fa9d..cd782f2 100644 --- a/filter.go +++ b/filter.go @@ -2,6 +2,7 @@ package underscore // Filter looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate). func Filter[T any](values []T, predicate func(T) bool) (res []T) { + res = make([]T, 0, len(values)) for _, v := range values { if predicate(v) { res = append(res, v) diff --git a/filter_test.go b/filter_test.go index fdd8e6b..a97d9f1 100644 --- a/filter_test.go +++ b/filter_test.go @@ -15,3 +15,16 @@ func TestFilter(t *testing.T) { want := []int{0, 2, 4, 6, 8} assert.Equal(t, want, u.Filter(nums, isEven)) } + +func BenchmarkFilter(b *testing.B) { + data := make([]int, 1000) + for i := range data { + data[i] = i + } + isEven := func(n int) bool { return n%2 == 0 } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + u.Filter(data, isEven) + } +} diff --git a/join_test.go b/join_test.go index 8cf322a..fbf8b95 100644 --- a/join_test.go +++ b/join_test.go @@ -23,7 +23,7 @@ func Test_Join_Can_Join_Two_Slices_Together(t *testing.T) { joined := u.Join(left, right, selector, selector) want := []u.Tuple[u.Tuple[int, string], []u.Tuple[int, string]]{ - {Left: zero, Right: nil}, + {Left: zero, Right: []u.Tuple[int, string]{}}, {Left: one, Right: []u.Tuple[int, string]{one}}, {Left: two, Right: []u.Tuple[int, string]{two, two}}, {Left: three, Right: []u.Tuple[int, string]{three, three, three}},