mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) optimization: remove lodash/pullAt
Summary: For a long array with removals proportional to that length, lodash/pullAt becomes slow due to doing one splice per removal. This diff swaps in an alternate implementation that doesn't become quadratic. On a 250k-row doc with a row-level access rule, this improves initial page load for a viewer with access to half the rows from minutes to seconds. Test Plan: added test; did manual benchmarking Reviewers: dsagal Reviewed By: dsagal Differential Revision: https://phab.getgrist.com/D2777
This commit is contained in:
@@ -577,6 +577,35 @@ export function mapToObject<T>(keysArray: string[], callback: (key: string) => T
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified elements from the array, with the elements specified by
|
||||
* their index. The array arr is modified in-place. The indexes must be provided
|
||||
* in order, sorted lowest to highest, with no duplicates, or out-of-bound indices,
|
||||
* etc (this method does no error checking; it is used in place of lodash-pullAt
|
||||
* for performance reasons).
|
||||
*/
|
||||
export function pruneArray<T>(arr: T[], indexes: number[]) {
|
||||
if (indexes.length === 0) { return; }
|
||||
if (indexes.length === 1) {
|
||||
arr.splice(indexes[0], 1);
|
||||
return;
|
||||
}
|
||||
const len = arr.length;
|
||||
let arrAt = 0;
|
||||
let indexesAt = 0;
|
||||
for (let i = 0; i < len; i++) {
|
||||
if (i === indexes[indexesAt]) {
|
||||
indexesAt++;
|
||||
continue;
|
||||
}
|
||||
if (i !== arrAt) {
|
||||
arr[arrAt] = arr[i];
|
||||
}
|
||||
arrAt++;
|
||||
}
|
||||
arr.length = arrAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* A List of python identifiers; the result of running keywords.kwlist in Python 2.7.6,
|
||||
* plus additional illegal identifiers None, False, True
|
||||
|
||||
Reference in New Issue
Block a user