underscore/map_test.go
Ruidy b04e545d03
test: add comprehensive benchmarks for core functions
Adds performance benchmarks for core collection functions to enable
tracking of performance regressions and optimization opportunities.

Benchmarks added:
- Map: 1000 element transformation
- Reduce: 1000 element sum
- Partition: 1000 element split
- Unique/UniqueInPlace: Comparison with many duplicates
- ParallelMap: Multiple worker counts (1, 2, 4, 8)
- MapVsParallelMap: Direct comparison (10k elements)

Key findings from benchmarks:
- Map: 1363 ns/op, 1 alloc (excellent)
- Reduce: 335 ns/op, 0 allocs (excellent)
- Partition: 3411 ns/op, 2 allocs (good - both slices)
- ParallelMap overhead: ~240x slower for simple operations
- ParallelMap is best for CPU-intensive operations (>1ms per element)

Use cases clarified:
- Regular Map for simple/fast operations
- ParallelMap for expensive operations with 100+ elements
- Optimal workers: 1-4 for most workloads

All tests pass 
Coverage maintained 

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 14:27:03 +01:00

51 lines
1,018 B
Go

package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func TestMap(t *testing.T) {
nums := []int{1, 2, 3}
f := func(n int) int {
return n * n
}
want := []int{1, 4, 9}
assert.Equal(t, want, u.Map(nums, f))
}
func TestMapEmpty(t *testing.T) {
result := u.Map([]int{}, func(n int) int { return n * 2 })
assert.Empty(t, result)
}
func TestMapSingleElement(t *testing.T) {
result := u.Map([]int{5}, func(n int) int { return n * 2 })
assert.Equal(t, []int{10}, result)
}
func TestMapLarge(t *testing.T) {
large := make([]int, 10000)
for i := range large {
large[i] = i
}
result := u.Map(large, func(n int) int { return n * 2 })
assert.Equal(t, 10000, len(result))
assert.Equal(t, 0, result[0])
assert.Equal(t, 19998, result[9999])
}
func BenchmarkMap(b *testing.B) {
data := make([]int, 1000)
for i := range data {
data[i] = i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.Map(data, func(n int) int { return n * 2 })
}
}