Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Using scriptTransformer cache in jest-runner
  • Loading branch information
Biki-das committed Jan 5, 2023
commit 3ec9e057e34a0cb71d0b0b3c9e895f54947beecb
38 changes: 14 additions & 24 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1574,23 +1574,18 @@ export default class Runtime {
return source;
}

let transformedFile: TransformResult | undefined =
this._fileTransforms.get(filename);

if (transformedFile) {
return transformedFile.code;
}

transformedFile = this._scriptTransformer.transform(
const transformedFile = this._scriptTransformer.transform(
filename,
this._getFullTransformationOptions(options),
source,
);

this._fileTransforms.set(filename, {
...transformedFile,
wrapperLength: this.constructModuleWrapperStart().length,
});
if (this._fileTransforms.get(filename)?.code !== transformedFile.code) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove this if and just always populate the cache. Then it's a single set instead of a single get and sometimes an additional set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that way we can prevent the additional set

Copy link
Member

@SimenB SimenB Jan 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a set isn't more expensive than get (just the extra spread in the object). and a single set is less expensive than get and set

this._fileTransforms.set(filename, {
...transformedFile,
wrapperLength: this.constructModuleWrapperStart().length,
});
}

if (transformedFile.sourceMapPath) {
this._sourceMapRegistry.set(filename, transformedFile.sourceMapPath);
Expand All @@ -1608,23 +1603,18 @@ export default class Runtime {
return source;
}

let transformedFile: TransformResult | undefined =
this._fileTransforms.get(filename);

if (transformedFile) {
return transformedFile.code;
}

transformedFile = await this._scriptTransformer.transformAsync(
const transformedFile = await this._scriptTransformer.transformAsync(
filename,
this._getFullTransformationOptions(options),
source,
);

this._fileTransforms.set(filename, {
...transformedFile,
wrapperLength: 0,
});
if (this._fileTransforms.get(filename)?.code !== transformedFile.code) {
this._fileTransforms.set(filename, {
...transformedFile,
wrapperLength: 0,
});
}

if (transformedFile.sourceMapPath) {
this._sourceMapRegistry.set(filename, transformedFile.sourceMapPath);
Expand Down