Skip to content

Commit 993c895

Browse files
Use array-of-sets
1 parent bd0bf21 commit 993c895

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

packages/tailwindcss/src/compile.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function compileCandidates(
5151
// variants used.
5252
let variantOrder = 0n
5353
for (let variant of candidate.variants) {
54-
let index = variants.findIndex((g) => g.findIndex((v) => v === variant) !== -1)
54+
let index = variants.findIndex((inner) => inner.has(variant))
5555
variantOrder |= 1n << BigInt(index)
5656
}
5757

@@ -324,7 +324,7 @@ function getPropertySort(nodes: AstNode[]) {
324324
}
325325

326326
/**
327-
* Sort an array of entries into arrays-of-arrays. Similar entries (where the
327+
* Sort an array of entries into an array-of-sets. Similar entries (where the
328328
* sort function returns 0) are grouped together. The sort function is stable.
329329
*
330330
* Example:
@@ -334,25 +334,32 @@ function getPropertySort(nodes: AstNode[]) {
334334
* Becomes:
335335
*
336336
* [
337-
* [1, 1],
338-
* [3],
339-
* [4],
337+
* Set[1, 1],
338+
* Set[3],
339+
* Set[4],
340340
* ]
341341
*
342342
* TODO: Make this faster.
343343
*/
344-
function sortAndGroup<T>(entries: T[], comparator: (a: T, z: T) => number): T[][] {
345-
let groups: T[][] = []
344+
function sortAndGroup<T>(entries: T[], comparator: (a: T, z: T) => number): Set<T>[] {
345+
let groups: Set<T>[] = []
346346
let sorted = entries.slice().sort(comparator)
347347

348348
for (let entry of sorted) {
349349
let prevGroup = groups[groups.length - 1]
350-
if (prevGroup && comparator(prevGroup[0], entry) === 0) {
351-
prevGroup.push(entry)
350+
if (prevGroup && comparator(first(prevGroup)!, entry) === 0) {
351+
prevGroup.add(entry)
352352
} else {
353-
groups.push([entry])
353+
groups.push(new Set([entry]))
354354
}
355355
}
356356

357357
return groups
358358
}
359+
360+
function first<T>(set: Set<T>): T | null {
361+
for (let value of set) {
362+
return value
363+
}
364+
return null
365+
}

0 commit comments

Comments
 (0)