1 | /*
|
2 | * Copyright 2022 The Backstage Authors
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 |
|
17 | const { default: JestRuntime } = require('jest-runtime');
|
18 |
|
19 | const scriptTransformCache = new Map();
|
20 |
|
21 | module.exports = class CachingJestRuntime extends JestRuntime {
|
22 | // This may or may not be a good idea. Theoretically I don't know why this would impact
|
23 | // test correctness and flakiness, but it seems like it may introduce flakiness and strange failures.
|
24 | // It does seem to speed up test execution by a fair amount though.
|
25 | createScriptFromCode(scriptSource, filename) {
|
26 | let script = scriptTransformCache.get(scriptSource);
|
27 | if (!script) {
|
28 | script = super.createScriptFromCode(scriptSource, filename);
|
29 | // Tried to store the script object in a WeakRef here. It starts out at
|
30 | // about 90% hit rate, but eventually drops all the way to 20%, and overall
|
31 | // it seemed to increase memory usage by 20% or so.
|
32 | scriptTransformCache.set(scriptSource, script);
|
33 | }
|
34 | return script;
|
35 | }
|
36 | };
|