perf: Prevent running regexs over the same strings twice

This commit is contained in:
Mark Molinaro 2021-11-10 01:22:09 +00:00 committed by GitHub
parent f011c70aac
commit 6d3cd42008
3 changed files with 38 additions and 6 deletions

View File

@ -5,6 +5,7 @@
"use strict";
const { groupBy } = require("./util/ArrayHelpers");
const createSchemaValidation = require("./util/create-schema-validation");
/** @typedef {import("../declarations/plugins/WatchIgnorePlugin").WatchIgnorePluginOptions} WatchIgnorePluginOptions */
@ -40,14 +41,12 @@ class IgnoringWatchFileSystem {
p instanceof RegExp ? p.test(path) : path.indexOf(p) === 0
);
const notIgnored = path => !ignored(path);
const ignoredFiles = files.filter(ignored);
const ignoredDirs = dirs.filter(ignored);
const [ignoredFiles, notIgnoredFiles] = groupBy(files, ignored);
const [ignoredDirs, notIgnoredDirs] = groupBy(dirs, ignored);
const watcher = this.wfs.watch(
files.filter(notIgnored),
dirs.filter(notIgnored),
notIgnoredFiles,
notIgnoredDirs,
missing,
startTime,
options,

View File

@ -12,3 +12,19 @@ exports.equals = (a, b) => {
}
return true;
};
/**
*
* @param {Array} arr Array of values to be partitioned
* @param {(value: any) => boolean} fn Partition function which partitions based on truthiness of result.
* @returns {[Array, Array]} returns the values of `arr` partitioned into two new arrays based on fn predicate.
*/
exports.groupBy = (arr = [], fn) => {
return arr.reduce(
(groups, value) => {
groups[fn(value) ? 0 : 1].push(value);
return groups;
},
[[], []]
);
};

View File

@ -0,0 +1,17 @@
"use strict";
const ArrayHelpers = require("../lib/util/ArrayHelpers");
describe("ArrayHelpers", () => {
it("groupBy should partition into two arrays", () => {
expect(
ArrayHelpers.groupBy([1, 2, 3, 4, 5, 6], x => x % 2 === 0)
).toStrictEqual([
[2, 4, 6],
[1, 3, 5]
]);
});
it("groupBy works with empty array", () => {
expect(ArrayHelpers.groupBy([], x => x % 2 === 0)).toStrictEqual([[], []]);
});
});