Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Casey <andrew.casey@microsoft.com>2020-06-03 04:18:58 +0300
committerMichaƫl Zasso <targos@protonmail.com>2021-04-26 10:28:39 +0300
commit5e77bcd3dc1433b95b30d248511c399e2eceed6c (patch)
tree34117cd1095ff2784107d175dd6ca0d5eaf53035 /benchmark
parent443cacee5faaac612b0a3fbecfff4f0073ed990c (diff)
lib: add throws option to fs.f/l/statSync
For consumers that aren't interested in *why* a `statSync` call failed, allocating and throwing an exception is an unnecessary expense. This PR adds an option that will cause it to return `undefined` in such cases instead. As a motivating example, the JavaScript & TypeScript language service shared between Visual Studio and Visual Studio Code is stuck with synchronous file IO for architectural and backward-compatibility reasons. It frequently needs to speculatively check for the existence of files and directories that may not exist (and cares about file vs directory, so `existsSync` is insufficient), but ignores file system entries it can't access, regardless of the reason. Benchmarking the language service is difficult because it's so hard to get good coverage of both code bases and user behaviors, but, as a representative metric, we measured batch compilation of a few hundred popular projects (by star count) from GitHub and found that, on average, we saved about 1-2% of total compilation time. We speculate that the savings could be even more significant in interactive (language service or watch mode) scenarios, where the same (non-existent) files need to be polled over and over again. It's not a huge improvement, but it's a very small change and it will affect a lot of users (and CI runs). For reference, our measurements were against `v12.x` (3637a061a at the time) on an Ubuntu Server desktop with an SSD. PR-URL: https://github.com/nodejs/node/pull/33716 Backport-PR-URL: https://github.com/nodejs/node/pull/36921 Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'benchmark')
-rw-r--r--benchmark/fs/bench-statSync-failure.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/benchmark/fs/bench-statSync-failure.js b/benchmark/fs/bench-statSync-failure.js
new file mode 100644
index 00000000000..82cb24c09f4
--- /dev/null
+++ b/benchmark/fs/bench-statSync-failure.js
@@ -0,0 +1,28 @@
+'use strict';
+
+const common = require('../common');
+const fs = require('fs');
+const path = require('path');
+
+const bench = common.createBenchmark(main, {
+ n: [1e6],
+ statSyncType: ['throw', 'noThrow']
+});
+
+
+function main({ n, statSyncType }) {
+ const arg = path.join(__dirname, 'non.existent');
+
+ bench.start();
+ for (let i = 0; i < n; i++) {
+ if (statSyncType === 'noThrow') {
+ fs.statSync(arg, { throwIfNoEntry: false });
+ } else {
+ try {
+ fs.statSync(arg);
+ } catch {
+ }
+ }
+ }
+ bench.end(n);
+}