Skip to content

Commit

Permalink
reset i correctly
Browse files Browse the repository at this point in the history
This makes the code more correct _and_ improves performance because the
`Number(…)` will now always deal with numbers.

On the tailwindcss.com codebase, sorting now goes from `~3.29ms` to
`~3.10ms`
  • Loading branch information
RobinMalfait committed Apr 11, 2024
1 parent 37d5bf8 commit 509e668
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/tailwindcss/src/utils/compare.ts
Expand Up @@ -19,13 +19,21 @@ export function compare(a: string, z: string) {

// If both are numbers, compare them as numbers instead of strings.
if (aCode >= ZERO && aCode <= NINE && zCode >= ZERO && zCode <= NINE) {
let initialI = i
let aStart = i
let zStart = i

// Consume the number
while (a.charCodeAt(i) >= ZERO && a.charCodeAt(i) <= NINE) i++
let aEnd = i

// Reset `i` to its initial value, this way we can find the end of number
// in `z`. If we don't do this, and use `i` as is it could be that we go
// past the number in `z`. A side effect of this is that the `Number()`
// call will be much slower than it needs to be because it will receive
// non-number like values.
i = initialI

// Consume the number
while (z.charCodeAt(i) >= ZERO && z.charCodeAt(i) <= NINE) i++
let zEnd = i
Expand Down

0 comments on commit 509e668

Please sign in to comment.