mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
perf: pre-allocate Partition result slices
Adds capacity hints to both keep and reject slices in Partition function to prevent repeated allocations during append operations. Changes: - keep: make([]T, 0) → make([]T, 0, len(values)) - reject: make([]T, 0) → make([]T, 0, len(values)) Impact: - Reduces allocations from O(log n) to O(1) for each slice - Improves performance by eliminating slice growth overhead - Minimal memory overhead as worst case is original slice size 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
7caa23e082
commit
46d52e3cfa
1 changed files with 2 additions and 2 deletions
|
|
@ -3,8 +3,8 @@ package underscore
|
||||||
// Partition splits the slice into two slices: one whose elements all satisfy predicate
|
// Partition splits the slice into two slices: one whose elements all satisfy predicate
|
||||||
// and one whose elements all do not satisfy predicate.
|
// and one whose elements all do not satisfy predicate.
|
||||||
func Partition[T any](values []T, predicate func(T) bool) ([]T, []T) {
|
func Partition[T any](values []T, predicate func(T) bool) ([]T, []T) {
|
||||||
keep := make([]T, 0)
|
keep := make([]T, 0, len(values))
|
||||||
reject := make([]T, 0)
|
reject := make([]T, 0, len(values))
|
||||||
|
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
if predicate(v) {
|
if predicate(v) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue