Skip to content

Commit 8755cdd

Browse files
committed
Improves performance of file.expand, file.expandMapping, and task.normalizeMultiTaskFiles by caching glob fs results across multiple calls to glob.sync
1 parent a09f84c commit 8755cdd

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/grunt/file.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ file.expand = function() {
9393
// If the first argument is an options object, save those options to pass
9494
// into the file.glob.sync method.
9595
var options = grunt.util.kindOf(args[0]) === 'object' ? args.shift() : {};
96+
// Cache fs calls across multiple file.glob.sync operations
97+
var globOptions = grunt.util._.extend(file._createGlobIoCache(), options);
9698
// Use the first argument if it's an Array, otherwise convert the arguments
9799
// object to an array and use that.
98100
var patterns = Array.isArray(args[0]) ? args[0] : args;
@@ -101,7 +103,7 @@ file.expand = function() {
101103
// Return all matching filepaths.
102104
var matches = processPatterns(patterns, function(pattern) {
103105
// Find all matching files for this pattern.
104-
return file.glob.sync(pattern, options);
106+
return file.glob.sync(pattern, globOptions);
105107
});
106108
// Filter result set?
107109
if (options.filter) {
@@ -452,3 +454,14 @@ file.isPathInCwd = function() {
452454
return false;
453455
}
454456
};
457+
458+
// Creates all glob options that store cached fs results for the `glob` library.
459+
// Can be removed when this glob PR is released: https://github.com/isaacs/node-glob/pull/306
460+
file._createGlobIoCache = function() {
461+
return {
462+
cache: Object.create(null),
463+
statCache: Object.create(null),
464+
symlinks: Object.create(null),
465+
realpathCache: Object.create(null)
466+
};
467+
};

lib/grunt/task.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ task.normalizeMultiTaskFiles = function(data, target) {
117117
}
118118

119119
// Process all normalized file objects.
120+
var globIoCache = grunt.file._createGlobIoCache();
120121
files = grunt.util._(files).chain().forEach(function(obj) {
121122
if (!('src' in obj) || !obj.src) { return; }
122123
// Normalize .src properties to flattened array.
@@ -127,7 +128,7 @@ task.normalizeMultiTaskFiles = function(data, target) {
127128
}
128129
}).map(function(obj) {
129130
// Build options object, removing unwanted properties.
130-
var expandOptions = grunt.util._.extend({}, obj);
131+
var expandOptions = grunt.util._.extend({}, globIoCache, obj);
131132
delete expandOptions.src;
132133
delete expandOptions.dest;
133134

0 commit comments

Comments
 (0)