Skip to content

Commit 8b36f23

Browse files
authored
fix(perf): cache this.exclude.shouldInstrument for improved performance (#388)
1 parent 3b0c578 commit 8b36f23

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

lib/report.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Report {
4444
excludeNodeModules: excludeNodeModules
4545
})
4646
this.excludeAfterRemap = excludeAfterRemap
47+
this.shouldInstrumentCache = new Map()
4748
this.omitRelative = omitRelative
4849
this.sourceMapCache = {}
4950
this.wrapperLength = wrapperLength
@@ -96,7 +97,7 @@ class Report {
9697
const path = resolve(this.resolve, v8ScriptCov.url)
9798
const converter = v8toIstanbul(path, this.wrapperLength, sources, (path) => {
9899
if (this.excludeAfterRemap) {
99-
return !this.exclude.shouldInstrument(path)
100+
return !this._shouldInstrument(path)
100101
}
101102
})
102103
await converter.load()
@@ -287,7 +288,7 @@ class Report {
287288
}
288289
}
289290
if ((!this.omitRelative || isAbsolute(v8ScriptCov.url))) {
290-
if (this.excludeAfterRemap || this.exclude.shouldInstrument(v8ScriptCov.url)) {
291+
if (this.excludeAfterRemap || this._shouldInstrument(v8ScriptCov.url)) {
291292
result.push(v8ScriptCov)
292293
}
293294
}
@@ -311,6 +312,23 @@ class Report {
311312
}
312313
return cache
313314
}
315+
316+
/**
317+
* this.exclude.shouldInstrument with cache
318+
*
319+
* @private
320+
* @return {boolean}
321+
*/
322+
_shouldInstrument (filename) {
323+
const cacheResult = this.shouldInstrumentCache.get(filename)
324+
if (cacheResult !== undefined) {
325+
return cacheResult
326+
}
327+
328+
const result = this.exclude.shouldInstrument(filename)
329+
this.shouldInstrumentCache.set(filename, result)
330+
return result
331+
}
314332
}
315333

316334
module.exports = function (opts) {

test/integration.js_10.snap

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ hey
179179
--------------------------|---------|----------|---------|---------|--------------------------------
180180
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
181181
--------------------------|---------|----------|---------|---------|--------------------------------
182-
All files | 73.72 | 59.03 | 62.5 | 73.72 |
182+
All files | 74.04 | 58.82 | 63.41 | 74.04 |
183183
bin | 78.84 | 60 | 66.66 | 78.84 |
184184
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
185-
lib | 78.32 | 54.38 | 72 | 78.32 |
185+
lib | 78.61 | 54.23 | 73.07 | 78.61 |
186186
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
187187
parse-args.js | 97.24 | 58.33 | 100 | 97.24 | 159-160,181-182,195-196
188-
report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313
188+
report.js | 76.19 | 57.89 | 80 | 76.19 | ...280,286-288,309-314,325-326
189189
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
190190
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
191191
check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70
@@ -194,9 +194,9 @@ All files | 73.72 | 59.03 | 62.5 | 73.72 |
194194
async.js | 100 | 100 | 100 | 100 |
195195
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
196196
--------------------------|---------|----------|---------|---------|--------------------------------
197-
,ERROR: Coverage for lines (73.72%) does not meet global threshold (101%)
198-
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
199-
ERROR: Coverage for statements (73.72%) does not meet global threshold (95%)
197+
,ERROR: Coverage for lines (74.04%) does not meet global threshold (101%)
198+
ERROR: Coverage for branches (58.82%) does not meet global threshold (82%)
199+
ERROR: Coverage for statements (74.04%) does not meet global threshold (95%)
200200
"
201201
`;
202202

@@ -214,9 +214,9 @@ ERROR: Coverage for branches (25%) does not meet threshold (82%) for lib/is-cjs-
214214
ERROR: Coverage for statements (90%) does not meet threshold (95%) for lib/is-cjs-esm-bridge.js
215215
ERROR: Coverage for lines (97.24%) does not meet threshold (101%) for lib/parse-args.js
216216
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/parse-args.js
217-
ERROR: Coverage for lines (75.47%) does not meet threshold (101%) for lib/report.js
218-
ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/report.js
219-
ERROR: Coverage for statements (75.47%) does not meet threshold (95%) for lib/report.js
217+
ERROR: Coverage for lines (76.19%) does not meet threshold (101%) for lib/report.js
218+
ERROR: Coverage for branches (57.89%) does not meet threshold (82%) for lib/report.js
219+
ERROR: Coverage for statements (76.19%) does not meet threshold (95%) for lib/report.js
220220
ERROR: Coverage for lines (45%) does not meet threshold (101%) for lib/source-map-from-file.js
221221
ERROR: Coverage for statements (45%) does not meet threshold (95%) for lib/source-map-from-file.js
222222
ERROR: Coverage for lines (100%) does not meet threshold (101%) for test/fixtures/async.js
@@ -227,19 +227,19 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt
227227
`;
228228

229229
exports[`c8 check-coverage check-coverage command with --100 1`] = `
230-
",,ERROR: Coverage for lines (77.51%) does not meet global threshold (100%)
231-
ERROR: Coverage for functions (66.66%) does not meet global threshold (100%)
232-
ERROR: Coverage for branches (62.35%) does not meet global threshold (100%)
233-
ERROR: Coverage for statements (77.51%) does not meet global threshold (100%)
230+
",,ERROR: Coverage for lines (77.75%) does not meet global threshold (100%)
231+
ERROR: Coverage for functions (67.44%) does not meet global threshold (100%)
232+
ERROR: Coverage for branches (62.06%) does not meet global threshold (100%)
233+
ERROR: Coverage for statements (77.75%) does not meet global threshold (100%)
234234
"
235235
`;
236236

237237
exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`;
238238

239239
exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = `
240-
",,ERROR: Coverage for lines (73.72%) does not meet global threshold (101%)
241-
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
242-
ERROR: Coverage for statements (73.72%) does not meet global threshold (95%)
240+
",,ERROR: Coverage for lines (74.04%) does not meet global threshold (101%)
241+
ERROR: Coverage for branches (58.82%) does not meet global threshold (82%)
242+
ERROR: Coverage for statements (74.04%) does not meet global threshold (95%)
243243
"
244244
`;
245245

@@ -326,13 +326,13 @@ exports[`c8 report generates report from existing temporary files 1`] = `
326326
",--------------------------|---------|----------|---------|---------|--------------------------------
327327
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
328328
--------------------------|---------|----------|---------|---------|--------------------------------
329-
All files | 73.72 | 59.03 | 62.5 | 73.72 |
329+
All files | 74.04 | 58.82 | 63.41 | 74.04 |
330330
bin | 78.84 | 60 | 66.66 | 78.84 |
331331
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
332-
lib | 78.32 | 54.38 | 72 | 78.32 |
332+
lib | 78.61 | 54.23 | 73.07 | 78.61 |
333333
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
334334
parse-args.js | 97.24 | 58.33 | 100 | 97.24 | 159-160,181-182,195-196
335-
report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313
335+
report.js | 76.19 | 57.89 | 80 | 76.19 | ...280,286-288,309-314,325-326
336336
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
337337
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
338338
check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70
@@ -348,13 +348,13 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
348348
",--------------------------|---------|----------|---------|---------|--------------------------------
349349
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
350350
--------------------------|---------|----------|---------|---------|--------------------------------
351-
All files | 73.72 | 59.03 | 62.5 | 73.72 |
351+
All files | 74.04 | 58.82 | 63.41 | 74.04 |
352352
bin | 78.84 | 60 | 66.66 | 78.84 |
353353
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
354-
lib | 78.32 | 54.38 | 72 | 78.32 |
354+
lib | 78.61 | 54.23 | 73.07 | 78.61 |
355355
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
356356
parse-args.js | 97.24 | 58.33 | 100 | 97.24 | 159-160,181-182,195-196
357-
report.js | 75.47 | 58.33 | 78.57 | 75.47 | ...251,278-279,285-287,308-313
357+
report.js | 76.19 | 57.89 | 80 | 76.19 | ...280,286-288,309-314,325-326
358358
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
359359
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
360360
check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70
@@ -363,9 +363,9 @@ All files | 73.72 | 59.03 | 62.5 | 73.72 |
363363
async.js | 100 | 100 | 100 | 100 |
364364
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
365365
--------------------------|---------|----------|---------|---------|--------------------------------
366-
,ERROR: Coverage for lines (73.72%) does not meet global threshold (101%)
367-
ERROR: Coverage for branches (59.03%) does not meet global threshold (82%)
368-
ERROR: Coverage for statements (73.72%) does not meet global threshold (95%)
366+
,ERROR: Coverage for lines (74.04%) does not meet global threshold (101%)
367+
ERROR: Coverage for branches (58.82%) does not meet global threshold (82%)
368+
ERROR: Coverage for statements (74.04%) does not meet global threshold (95%)
369369
"
370370
`;
371371

0 commit comments

Comments
 (0)