开玩笑的定制变压器-它的性能可以提高吗?

Kul*_*Kul 5 jestjs

我正在用Jest测试定制变压器的性能。当前,转换器不执行任何操作,仅返回从Jest获取的代码。变压器实现了该getCacheKey功能。

这是变压器的完整代码:

function process(src, path, config, transformOptions) {
  return src;
}
exports.process = process;

function getCacheKey(fileData, filePath, configStr, options) {
  return crypto.createHash('md5')
    .update(fileData + filePath + configStr, 'utf8')
    .digest('hex');
}
exports.getCacheKey = getCacheKey;
Run Code Online (Sandbox Code Playgroud)

链接到变压器

开玩笑的配置,package.json如下所示:

"jest": {
  "transform": {
    "^.+\\.tsx?$": "<rootDir>/ts-transformer.js"
  },
  "testMatch": [
    "<rootDir>/test-jest/**/*.ts"
  ],
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "json"
  ]
}
Run Code Online (Sandbox Code Playgroud)

链接到package.json

使用Jest测试此设置时,无论有无,它花费的时间都是相同的--no-cache(大约9秒)

使用Mocha测试此设置时,第一次运行大约需要7秒钟,随后的运行大约需要4秒钟。

在这两种情况下(带有笑话和摩卡),都在不更改任何源文件或测试文件的情况下测试了后续运行。

我的问题:

  • 后续的Jest运行不应该由于缓存而更快吗?
  • 变压器中是否有某种东西在阻止测试时间的延长?
  • Jest产生的最低开销是否使这个问题蒙上阴影?

zap*_*aph 0

单独更新各个部分(fileData、filePath、configStr)可能会更快,因此串联时不必有文件内容的副本。

function getCacheKey(fileData, filePath, configStr, options) {
    const hash = crypto.createHash('md5');
    hash.update(fileData);
    hash.update(filePath);
    hash.update(configStr);
    return hash.digest('hex');
}
Run Code Online (Sandbox Code Playgroud)

注意:如果未提供编码,并且数据是字符串,则强制使用“utf8”编码。