Skip to content

Commit 93377f7

Browse files
committed
refactor needRebuild to make use of early return if possible
1 parent 128e043 commit 93377f7

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

‎lib/NormalModule.js‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class NormalModule extends Module {
163163
this.fileDependencies = result.fileDependencies;
164164
this.contextDependencies = result.contextDependencies;
165165
}
166+
166167
if(err) {
167168
return callback(this.error = new ModuleBuildError(this, err));
168169
}
@@ -319,19 +320,39 @@ class NormalModule extends Module {
319320
return new CachedSource(source);
320321
}
321322

323+
getHighestTimestamp(keys, timestampsByKey) {
324+
let highestTimestamp = 0;
325+
for(let i = 0; i < keys.length; i++) {
326+
const key = keys[i];
327+
const timestamp = timestampsByKey[key];
328+
// if there is no timestamp yet, early return with Infinity
329+
if(!timestamp) return Infinity;
330+
highestTimestamp = Math.max(highestTimestamp, timestamp);
331+
}
332+
return highestTimestamp;
333+
}
334+
322335
needRebuild(fileTimestamps, contextTimestamps) {
323-
let timestamp = 0;
324-
this.fileDependencies.forEach(function(file) {
325-
const ts = fileTimestamps[file];
326-
if(!ts) timestamp = Infinity;
327-
if(ts > timestamp) timestamp = ts;
328-
});
329-
this.contextDependencies.forEach(function(context) {
330-
const ts = contextTimestamps[context];
331-
if(!ts) timestamp = Infinity;
332-
if(ts > timestamp) timestamp = ts;
333-
});
334-
return timestamp >= this.buildTimestamp;
336+
const highestFileDepTimestamp = this.getHighestTimestamp(
337+
this.fileDependencies, fileTimestamps);
338+
// if the hightest is Infinity, we need a needRebuild
339+
// exit early here.
340+
if(highestFileDepTimestamp === Infinity) {
341+
return true;
342+
}
343+
344+
const highestContextDepTimestamp = this.getHighestTimestamp(
345+
this.contextDependencies, contextTimestamps);
346+
347+
// Again if the hightest is Infinity, we need a needRebuild
348+
// exit early here.
349+
if(highestContextDepTimestamp === Infinity) {
350+
return true;
351+
}
352+
353+
// else take the highest of file and context timestamps and compare
354+
// to last build timestamp
355+
return Math.max(highestContextDepTimestamp, highestFileDepTimestamp) >= this.buildTimestamp;
335356
}
336357

337358
size() {

0 commit comments

Comments
 (0)