Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I was profiling a dashboard that loads ~100k rows from a CSV and sorts and bins them by a date column (
node --cpu-prof), andd3.sort(data, d => d.date)alone took about half a second.Comparing two Dates with
<or>=goes through ToPrimitive on both sides every time, which is way slower than comparing numbers, and sort, bin and extent do several comparisons per value. Sorting 100k Dates with a plaina < bcomparator is 6-8x slower than sorting the same values as numbers, in V8, SpiderMonkey and JSC alike.This converts Dates once with
valueOf()before comparing, through a tinyprimitivehelper, in ascending, descending, ascendingDefined (so sort, quickselect, rank etc. get it too), extent, min, max, minIndex, maxIndex and bin. Returned values are still the original Date objects, and since<on a Date already comparesvalueOf()the order is the same, null and Invalid Date included.Tests pass, and I fuzzed ~8.6k random inputs (Dates, null, Invalid Date, NaN, strings, mixed types) against main with identical results.
Numbers
M1 Pro, node 26, main
24c015e, median of 5 runs of the harness below:sort(data, d => d.date)extent(data, d => d.date)bin().value(d => d.date).thresholds(months)sort(data, d => d.value)Number sorts don't get slower (last row), the extra
instanceofis basically free.harness
Save as
bench.jsin the repo root and runnode bench.json main and on this branch.