grantila / grantila/fast-string-compare
Isn't this just a less performant version of `a > b ? 1 : b > a ? -1 : 0`? (Also: `localeCompare` is now faster too)
- Dominant language
- TypeScript
- Stars
- 2
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
This now appears to run slightly slower than `localeCompare` in V8, presumably due to `localeCompare` being better optimized than it was before. More to the point, it's hard to see how it could ever be faster than the "naive" `a > b ? 1 : b > a ? -1 : 0`, which I think should yield identical results for all cases, plus it stands to reason it would be the most optimized of all.
Some quick benchmarks seem to bear this out (`deno 2.0.0, v8 12.9.202.13-rusty`):
```ts
import { compare as fastStringCompare } from 'npm:fast-string-compare'
import { assertEquals } from 'jsr:@std/assert/equals'
function localeCompare(a: string, b: string) {
return a.localeCompare(b, 'en-US')
}
function naive(a: string, b: string) {
return a > b ? 1 : b > a ? -1 : 0
}
const fns = {
fastStringCompare,
localeCompare,
naive,
}
for (const [name, fn] of Object.entries(fns)) {
Deno.bench(name, () => {
const a = 'a'
const b = 'b'
const c = 'c'
for (const prefix of ['', a, b, c, 'aaaaaaaaaaaaaa', 'xxxxxxxxxxx', 'abcdefgh', 'gfdjkhgf', '返回德国空军法国很快就', '🍸🍻🍸🍹🌭🥪🎂🎈🌞']) {
assertEquals(fn(prefix + a, prefix + b), -1)
assertEquals(fn(prefix + a, prefix + c), -1)
assertEquals(fn(prefix + b, prefix + a), 1)
assertEquals(fn(prefix + a, prefix + a), 0)
assertEquals(fn(prefix + a, prefix), 1)
assertEquals(fn(prefix, prefix + a), -1)
assertEquals(fn(prefix + a + prefix, prefix + b + prefix), -1)
assertEquals(fn(prefix + a + prefix, prefix + c + prefix), -1)
assertEquals(fn(prefix + b + prefix, prefix + a + prefix), 1)
assertEquals(fn(prefix + a + prefix, prefix + a + prefix), 0)
}
})
}
```
```
benchmark time/iter (avg) iter/s (min … max) p75 p99 p995
------------------- ----------------------------- --------------------- --------------------------
fastStringCompare 36.5 µs 27,410 ( 24.7 µs … 1.5 ms) 32.4 µs 194.6 µs 262.5 µs
localeCompare 20.0 µs 49,880 ( 10.6 µs … 1.0 ms) 18.4 µs 173.7 µs 235.0 µs
naive 14.1 µs 70,760 ( 9.2 µs … 1.5 ms) 11.7 µs 75.8 µs 189.2 µs
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.