Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add components.MergeClasses #233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Add components.MergeClasses #233

wants to merge 1 commit into from

Conversation

markuswustenberg
Copy link
Member

Copy link

@apetsko apetsko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, just little faster implementation

var result []string
parts := strings.Split(strings.Join(classes, " "), " ")
for _, p := range parts {
	if p == "" || slices.Contains(result, p) {
		continue
	}
	result = append(result, p)
}
sort.Strings(result)
return strings.Join(result, " ")

@pdf
Copy link

pdf commented Oct 18, 2024

I'm undecided if this is a good idea or not yet, but on the implementation, do we care about duplicates? The current implementation for Classes doesn't dedup.

If we do want dedup, this implentation significantly more efficient:

func MergeClasses(classes ...string) string {
        var results []string
        for _, c := range classes {
                results = append(results, strings.Split(c, " ")...)
        }

        sort.Strings(results)
        return Class(strings.Join(slices.Compact(results), " "))
}
BenchmarkDefaultNonOverlapping-16         370407              3789 ns/op            2558 B/op         11 allocs/op
BenchmarkDefaultOverlapping-16            292194              3917 ns/op            2670 B/op         11 allocs/op
BenchmarkApetskoNonOverlapping-16         588950              2366 ns/op            1552 B/op          9 allocs/op
BenchmarkApetskoOverlapping-16            408938              3022 ns/op            1728 B/op          9 allocs/op
BenchmarkSlicesNonOverlapping-16         1501532               800.7 ns/op          1104 B/op          5 allocs/op *
BenchmarkSlicesOverlapping-16             864552              1285 ns/op            1376 B/op          5 allocs/op *

If we don't care about duplicates, this func just becomes a simple return Class(strings.Join(classes, " ")) (unless class sorting is also important, I see that is part of the Classes implementation).

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.

3 participants