summaryrefslogtreecommitdiff
path: root/benchmark
diff options
context:
space:
mode:
authorAndrew Casey <andrew.casey@microsoft.com>2020-06-03 01:18:58 +0000
committerMyles Borins <mylesborins@github.com>2020-11-18 16:19:33 -0500
commit3772cf56ae289e4cdc456e576eba43311bab24c8 (patch)
treef7dba2be1c96c199bf8ddaec4ef21db2b96ba03b /benchmark
parentb3b0c4347426094afe48c9e2562881e3e2b0c186 (diff)
downloadios-node-v8-3772cf56ae289e4cdc456e576eba43311bab24c8.tar.gz
ios-node-v8-3772cf56ae289e4cdc456e576eba43311bab24c8.tar.bz2
ios-node-v8-3772cf56ae289e4cdc456e576eba43311bab24c8.zip
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 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 0000000000..82cb24c09f
--- /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);
+}