8000 Merge pull request #14694 from markjm/markjm/double-regexp · webpack/webpack@e6da1d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6da1d4

Browse files
authored
Merge pull request #14694 from markjm/markjm/double-regexp
perf: Prevent running regexs over the same strings twice
2 parents f011c70 + 6d3cd42 commit e6da1d4

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

lib/WatchIgnorePlugin.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
"use strict";
77

8+
const { groupBy } = require("./util/ArrayHelpers");
89
const createSchemaValidation = require("./util/create-schema-validation");
910

1011
/** @typedef {import("../declarations/plugins/WatchIgnorePlugin").WatchIgnorePluginOptions} WatchIgnorePluginOptions */
@@ -40,14 +41,12 @@ class IgnoringWatchFileSystem {
4041
p instanceof RegExp ? p.test(path) : path.indexOf(p) === 0
4142
);
4243

43-
const notIgnored = path => !ignored(path);
44-
45-
const ignoredFiles = files.filter(ignored);
46-
const ignoredDirs = dirs.filter(ignored);
44+
const [ignoredFiles, notIgnoredFiles] = groupBy(files, ignored);
45+
const [ignoredDirs, notIgnoredDirs] = groupBy(dirs, ignored);
4746

4847
const watcher = this.wfs.watch(
49-
files.filter(notIgnored),
50-
dirs.filter(notIgnored),
48+
notIgnoredFiles,
49+
notIgnoredDirs,
5150
missing,
5251
startTime,
5352
options,

lib/util/ArrayHelpers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,19 @@ exports.equals = (a, b) => {
1212
}
1313
return true;
1414
};
15+
16+
/**
17+
*
18+
* @param {Array} arr Array of values to be partitioned
19+
* @param {(value: any) => boolean} fn Partition function which partitions based on truthiness of result.
20+
* @returns {[Array, Array]} returns the values of `arr` partitioned into two new arrays based on fn predicate.
21+
*/
22+
exports.groupBy = (arr = [], fn) => {
23+
return arr.reduce(
24+
(groups, value) => {
25+
groups[fn(value) ? 0 : 1].push(value);
26+
return groups;
27+
},
28+
[[], []]
29+
);
30+
};

test/ArrayHelpers.unittest.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
3+
const ArrayHelpers = require("../lib/util/ArrayHelpers");
4+
5+
describe("ArrayHelpers", () => {
6+
it("groupBy should partition into two arrays", () => {
7+
expect(
8+
ArrayHelpers.groupBy([1, 2, 3, 4, 5, 6], x => x % 2 === 0)
9+
).toStrictEqual([
10+
[2, 4, 6],
11+
[1, 3, 5]
12+
]);
13+
});
14+
it("groupBy works with empty array", () => {
15+
expect(ArrayHelpers.groupBy([], x => x % 2 === 0)).toStrictEqual([[], []]);
16+
});
17+
});

0 commit comments

Comments
 (0)
0