Open lionel-rowe opened 1 week ago
Hmm... when adding my naive
function and playing around the various included benchmarks/index.ts
in Node, Deno, and Bun, the results seem to vary a lot depending on the environment, how the functions are being run, array size, etc. The outlier is localeCompare
in Bun, which has atrocious performance for some reason (the other results in Bun are good, even for Intl.Collator
, which is surprising as I'd expect it to be near-identical to localeCompare
). Generally speaking fast
tends to be slightly more performant than naive
and similar or slightly slower than localeCompare
.
I also added a naiveEqFirst
version, which I expected to either be either near-identical to naive
or to outperform it slightly (due to never needing to check b > a
). The expected result seems to hold true, but even that isn't completely stable across runs (occasionally it's slower than naive
).
function naiveEqFirst(a: string, b: string) {
return a === b ? 0 : a > b ? 1 : -1
}
It still seems intuitive to me that naive
and/or naiveEqFirst
would be fastest as they just use basic operators, and it also seems intuitive that it would have the highest theoretical ceiling for perf given that the comparison is implemented in the engine itself (while also being less complex vs localeCompare
), but I guess that isn't necessarily the case.
This now appears to run slightly slower than
localeCompare
in V8, presumably due tolocaleCompare
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
):