enhanced-resolve icon indicating copy to clipboard operation
enhanced-resolve copied to clipboard

Use throwIfNoEntry for sync resolver once graceful-fs supports it

Open markjm opened this issue 4 years ago • 1 comments

For cases of using sync resolver, throwIfNoEntry improves stat fail case by a large margin. The API in node is backwards compatible, so we should just be able to add this option and be on our way.

Unfortunately, there is an issue with this option in graceful-fs, so I have opened an issue there. Once resolved, this should be unblocked and easy change here.

https://github.com/isaacs/node-graceful-fs/issues/221

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"); // 6.7s

function newStat(path) {
  let stat = fs.statSync(path, { throwIfNoEntry: false });

}

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

markjm avatar Nov 01 '21 18:11 markjm

Make sense, PR welcome

alexander-akait avatar Nov 02 '21 10:11 alexander-akait