question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Perf improvement to stat in jest-resolve

See original GitHub issue

🐛 Bug Report

jest-resolve specified its own isFile and isDirectory. stat[Sync] does some weird stuff by capturing a very large stack trace on failure (see here). Based on what we need from the errors of statSync for resolving, we dont need these stack traces (as errors when resolving are much more “expected”). My proposal below should improve resolving performance (with included rough console.time numbers):

To Reproduce

Create a “simulated” resolve environment (just forcing a lot of failed stat calls)

const fs = require("fs");
function oldStat(path) {
  let stat;

  try {
    stat = fs.statSync(path);
  } catch (e) {
    if (!(e && (e.code === "ENOENT" || e.code === "ENOTDIR"))) {
      throw e;
    }
  }
}

console.time("oldStat");
for (let i = 0; i < 100000; i++) oldStat("not-real-path");
console.timeEnd("oldStat");


# Console output
oldStat: 8.336s

Expected behavior

If we prevent capturing these extended traces, we see much improved CPU time for these stat calls.

function newStat(path) {
  let stat;
  let oldTrace = Object.getOwnPropertyDescriptors(Error).stackTraceLimit;

  try {
    Object.defineProperty(Error, "stackTraceLimit", {
      value: 0,
      writable: false,
    });
    stat = fs.statSync(path);
  } catch (e) {
    if (!(e && (e.code === "ENOENT" || e.code === "ENOTDIR"))) {
      throw e;
    }
  } finally {
    Object.defineProperty(Error, "stackTraceLimit", oldTrace);
  }
}

console.time("newStat");
for (let i = 0; i < 100000; i++) newStat("not-real-path");
console.timeEnd("newStat");

# Console output
newStat: 5.014s

Is there any interest in this change? If so, I can make a simple PR to get this going. Apologies if this doesnt dall in “bug” work item type, but didnt think it was a feature either.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sokracommented, Aug 10, 2021

This only affects statSync. In very recent node.js versions there is an option to return undefined instead of throwing an error on not found

0reactions
github-actions[bot]commented, Feb 6, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jest-resolve | Yarn - Package Manager
Fixed mocking of fields that start with an underscore ("private fields"). Fixed unmocking behavior with npm3. Fixed and improved --onlyChanged option.
Read more >
0 - Stack Overflow
I've been trying to find a way to test my TypeScript project but with every try Jest or typescript have yelled at me....
Read more >
Introducing Yarn 2 ! - DEV Community ‍ ‍
I perf was one of the many reasons to switch to yarn, I'm curious to know if if there continues to be improvements...
Read more >
JavaScript Unit Testing Performance - Jest
perf -improved-scheduling. Because we are running slow tests first, Jest can sometimes seem to take a long time to start up – we...
Read more >
Relay
Relay is designed for high performance at any scale. ... built in tandem with GraphQL and has full-time staff working to improve it....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found