To fix, simply change the !== undefined to === undefined.
You could also swap the clauses in the ternary expression instead.
I tested this and it works.
That will filter out any undefineds, or return an empty array if the first element is undefined. null gets sorted to the the beginning of the array.
This allows for having undefined at the beginning of the array. Not as elegant, but required due to x being undefined if an empty array is passed in (which is when the quicksort recursion ends and starts "undoing").
I did it with just .sort() (labeled as native), but I'm not sure how that compares.
It does affect the sort order though: plain .sort() orders it [numbers, nulls, undefineds], while this orders it like the other implementations, with undefineds being put at the end instead of being filtered, i.e. [nulls, numbers, undefineds].
Feel free to write some more benchmarks yourself, if you want :).
I did those on my Pi 4 4GB, which is slowish.
It gets 24k ops/s for native sort while my phone (Galaxy A51 4GB) gets 30k ops/s.
Update
I remembered something "quirky" about JavaScript's default sorting algorithm: it coerces values to strings and sorts them lexicographically.
The reason the plain sort worked was because all then numbers had the same number of digits. If they had varying numbers, it would have sorted them like this: [2, 20, 3, 30].
I have made updated JSBen.ch benchmarks using arr.sort((a, b) => a - b) (I can't manage to login to JSBench.me):
Just a question, I may be missing something.
You said
Yet your code is:
This will return an empty array when x is not undefined, and do more sorting if it is undefined.
Indeed, I ran the following code, and it logged
[]
.To fix, simply change the
!== undefined
to=== undefined
.You could also swap the clauses in the ternary expression instead.
I tested this and it works.
That will filter out any
undefined
s, or return an empty array if the first element isundefined
.null
gets sorted to the the beginning of the array.Thanks for the catch! I'll update!
Update:
I played around with it and came up with this:
This allows for having
undefined
at the beginning of the array. Not as elegant, but required due tox
beingundefined
if an empty array is passed in (which is when the quicksort recursion ends and starts "undoing").I did a few runs in JSBen.ch using my Pi 4 4GB.
JSBen.ch test.
JSBench.me was less favorable.
I get this:
JSBench.me test
Note: see updated benchmark code in my later comments.
Or use nested ternary operators:
I tried to figure out something like that, but got stuck.
Much better, thanks!
(Now to rerun the benchmarks...)
New benchmark results:
Run 1:
Run 2:
New JSBen.ch test
P.S. I dunno why I have got so stuck into this benchmarking :).
Cool! Thanks for adding those benchmarks! I wonder how it compares to this version:
I did it with just
.sort()
(labeled as native), but I'm not sure how that compares.It does affect the sort order though: plain
.sort()
orders it[numbers, nulls, undefineds]
, while this orders it like the other implementations, withundefined
s being put at the end instead of being filtered, i.e.[nulls, numbers, undefineds]
.Feel free to write some more benchmarks yourself, if you want :).
I did those on my Pi 4 4GB, which is slowish.
It gets 24k ops/s for native sort while my phone (Galaxy A51 4GB) gets 30k ops/s.
Update
I remembered something "quirky" about JavaScript's default sorting algorithm: it coerces values to strings and sorts them lexicographically.
The reason the plain sort worked was because all then numbers had the same number of digits. If they had varying numbers, it would have sorted them like this:
[2, 20, 3, 30]
.I have made updated JSBen.ch benchmarks using
arr.sort((a, b) => a - b)
(I can't manage to login to JSBench.me):