Skip to content

Commit 73e3e22

Browse files
committed
Improve Color.Equals performance from O(n²) to O(n)
1 parent 4c05561 commit 73e3e22

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

color.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -471,27 +471,24 @@ func (c *Color) Equals(c2 *Color) bool {
471471
if c == nil || c2 == nil {
472472
return false
473473
}
474+
474475
if len(c.params) != len(c2.params) {
475476
return false
476477
}
477478

479+
counts := make(map[Attribute]int, len(c.params))
478480
for _, attr := range c.params {
479-
if !c2.attrExists(attr) {
480-
return false
481-
}
481+
counts[attr]++
482482
}
483483

484-
return true
485-
}
486-
487-
func (c *Color) attrExists(a Attribute) bool {
488-
for _, attr := range c.params {
489-
if attr == a {
490-
return true
484+
for _, attr := range c2.params {
485+
if counts[attr] == 0 {
486+
return false
491487
}
488+
counts[attr]--
492489
}
493490

494-
return false
491+
return true
495492
}
496493

497494
func boolPtr(v bool) *bool {

0 commit comments

Comments
 (0)