We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 2382197 + daaca16 commit 54f8276Copy full SHA for 54f8276
1 file changed
types.go
@@ -159,8 +159,20 @@ func (fs *fields) OmitEmpty(e *Encoder, strct reflect.Value) []*field {
159
return fs.List
160
}
161
162
- fields := make([]*field, 0, len(fs.List))
+ // First pass: count surviving fields. If all survive, return the
163
+ // original slice without allocating a filtered copy.
164
+ n := 0
165
+ for _, f := range fs.List {
166
+ if !f.Omit(e, strct) {
167
+ n++
168
+ }
169
170
+ if n == len(fs.List) {
171
+ return fs.List
172
173
174
+ // Second pass: build the filtered slice only when necessary.
175
+ fields := make([]*field, 0, n)
176
for _, f := range fs.List {
177
if !f.Omit(e, strct) {
178
fields = append(fields, f)
0 commit comments