Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JestRuntime:-Using scriptTransformer cache in jest-runner #13735

Merged
merged 3 commits into from Jan 7, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 14 additions & 25 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -42,7 +42,6 @@ import {
CallerTransformOptions,
ScriptTransformer,
ShouldInstrumentOptions,
TransformResult,
TransformationOptions,
handlePotentialSyntaxError,
shouldInstrument,
Expand Down Expand Up @@ -1574,23 +1573,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 +1602,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