Skip to content

perf: use Ternary() instead If().Else() in Range/RangeFrom#770

Merged
samber merged 1 commit intosamber:masterfrom
d-enk:optimize-range
Jan 13, 2026
Merged

perf: use Ternary() instead If().Else() in Range/RangeFrom#770
samber merged 1 commit intosamber:masterfrom
d-enk:optimize-range

Conversation

@d-enk
Copy link
Contributor

@d-enk d-enk commented Jan 12, 2026

No allocs in it.Range, it.RangeFrom

For Benchmarks like:

func BenchmarkRange(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for range Range(0) {
		}
	}
}
pkg: github.com/samber/lo

            │   old.txtnew.txt                │
            │    sec/opsec/op     vs baseRangeFrom-4   6.9015n ± 1%   0.2802n ± 1%  -95.94% (p=0.000 n=10)
Range-4       6.8400n ± 1%   0.2803n ± 0%  -95.90% (p=0.000 n=10)

            │   old.txtnew.txt               │
            │     B/opB/op     vs baseRangeFrom-4   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10)
Range-4       0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10)

            │   old.txtnew.txt               │
            │  allocs/opallocs/op   vs baseRangeFrom-4   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10)
Range-4       0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=10)

pkg: github.com/samber/lo/it
            │    old.txtnew.txt                │
            │     sec/opsec/op      vs baseRangeFrom-4   85.8800n ±  2%   0.2802n ±  1%  -99.67% (p=0.000 n=10)
Range-4       67.8400n ± 13%   0.2807n ± 32%  -99.59% (p=0.000 n=10)

            │  old.txtnew.txt                 │
            │    B/opB/op     vs baseRangeFrom-4   72.00 ± 0%   0.00 ± 0%  -100.00% (p=0.000 n=10)
Range-4       48.00 ± 0%   0.00 ± 0%  -100.00% (p=0.000 n=10)

            │  old.txtnew.txt                 │
            │ allocs/opallocs/op   vs baseRangeFrom-4   3.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=10)
Range-4       3.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=10)

@codecov
Copy link

codecov bot commented Jan 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.22%. Comparing base (a2553e5) to head (2116531).
⚠️ Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #770      +/-   ##
==========================================
- Coverage   94.23%   94.22%   -0.02%     
==========================================
  Files          18       18              
  Lines        2880     2872       -8     
==========================================
- Hits         2714     2706       -8     
  Misses        150      150              
  Partials       16       16              
Flag Coverage Δ
unittests 94.22% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@samber
Copy link
Owner

samber commented Jan 12, 2026

i suppose it would be faster in a single condition ?

No allocs in it.Range, it.RangeFrom

Add more tests
@d-enk
Copy link
Contributor Author

d-enk commented Jan 13, 2026

i suppose it would be faster in a single condition ?

Well, length := elementNum * step is definitely cleaner.

@samber samber merged commit 2859d73 into samber:master Jan 13, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants