-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Retrieve the number of groups to compute. | ||
// Groups are divided into clusters of elements. | ||
// Each group has `CLUSTER_FACTOR` more elements per cluster than the previous | ||
// one. | ||
export const getGroupsCount = function (length) { | ||
return Math.floor( | ||
Math.log(length / MIN_GROUP_SIZE) / Math.log(CLUSTER_FACTOR), | ||
) | ||
} | ||
|
||
// Retrieve the `clusterSize`, i.e. number of elements per cluster, of each | ||
// group. | ||
export const getClusterSizes = function (groupsCount) { | ||
return Array.from({ length: groupsCount }, getClusterSize) | ||
} | ||
|
||
const getClusterSize = function (_, index) { | ||
return CLUSTER_FACTOR ** (index + 1) | ||
} | ||
|
||
// Minimum `groupSize` | ||
// A higher value lowers accuracy: | ||
// - The result `envDev` will be lower than the real value | ||
// - This is because more `array` elements are required to reach the "optimal" | ||
// size. | ||
// - This means multiplying this constant by `n` requires running the benchmark | ||
// `n` times longer to get the same `envDev` | ||
// A lower value lowers precision: | ||
// - This is because groups with a lower groupSize are less precise | ||
// - This is especially visible in preview mode, especially when a new group | ||
// is added | ||
// - This is because the last group are less precise. | ||
// - Also, new groups generally have higher `varianceRatio` if the "optimal" | ||
// size has not been reached yet, so each new group will make `envDev` | ||
// increase until it reaches its optimal value. | ||
// In general, `envDev` tends to be generally too low, so we favor accuracy over | ||
// precision. | ||
// However, this does mean `envDev` tends to vary quite a lot between different | ||
// `array`. | ||
const MIN_GROUP_SIZE = 2 | ||
|
||
// Each group has `CLUSTER_FACTOR` more elements per cluster than the previous | ||
// one. | ||
// A lower value: | ||
// - Is slower to compute | ||
// - Using `CLUSTER_FACTOR ** n` divides the time complexity by `sqrt(n)` | ||
// - Leads to an overall slightly worse accuracy | ||
// A higher value: | ||
// - Leads to much poorer accuracy and precision when the "optimal" size is | ||
// close to the `array.length` | ||
// - Specifically when that "optimal" size is higher than | ||
// `array.length` / (CLUSTER_FACTOR ** 2) | ||
// We must also ensure that `CLUSTER_FACTOR ** MAX_ARGUMENTS >= MAX_SAMPLES` | ||
// - MAX_ARGUMENTS is the maximum number of arguments to Math.max(): 123182 | ||
// - MAX_SAMPLES is the maximum number of array elements: 123182 | ||
// - Otherwise, `Math.max(...groups)` would crash | ||
// Using an integer >= 2 allows several implementation performance optimizations | ||
export const CLUSTER_FACTOR = 2 |
This file contains 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