Commit 15ff155
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` (3637a06 at the
time) on an Ubuntu Server desktop with an SSD.
PR-URL: #33716
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>1 parent e48ec70 commit 15ff155Copy full SHA for 15ff155
File tree
Expand file treeCollapse file tree
4 files changed
+84
-3
lines changedOpen diff view settings
Filter options
- benchmark/fs
- doc/api
- lib
- test/parallel
Expand file treeCollapse file tree
4 files changed
+84
-3
lines changedOpen diff view settings
Collapse file
benchmark/fs/bench-statSync-failure.js
Copy file name to clipboard+28Lines changed: 28 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
Collapse file
+6Lines changed: 6 additions & 0 deletions
- Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
2568 | 2568 | |
2569 | 2569 | |
2570 | 2570 | |
| 2571 | + |
| 2572 | + |
| 2573 | + |
2571 | 2574 | |
2572 | 2575 | |
2573 | 2576 | |
| ||
3810 | 3813 | |
3811 | 3814 | |
3812 | 3815 | |
| 3816 | + |
| 3817 | + |
| 3818 | + |
3813 | 3819 | |
3814 | 3820 | |
3815 | 3821 | |
|
Collapse file
+23-3Lines changed: 23 additions & 3 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
72 | 72 | |
73 | 73 | |
74 | 74 | |
| 75 | + |
75 | 76 | |
76 | 77 | |
77 | 78 | |
| ||
1076 | 1077 | |
1077 | 1078 | |
1078 | 1079 | |
1079 | | - |
| 1080 | + |
| 1081 | + |
| 1082 | + |
| 1083 | + |
| 1084 | + |
| 1085 | + |
| 1086 | + |
| 1087 | + |
| 1088 | + |
| 1089 | + |
| 1090 | + |
| 1091 | + |
| 1092 | + |
| 1093 | + |
1080 | 1094 | |
1081 | 1095 | |
1082 | 1096 | |
1083 | 1097 | |
1084 | 1098 | |
1085 | 1099 | |
1086 | 1100 | |
1087 | | - |
| 1101 | + |
1088 | 1102 | |
1089 | 1103 | |
1090 | 1104 | |
1091 | 1105 | |
| 1106 | + |
| 1107 | + |
| 1108 | + |
1092 | 1109 | |
1093 | 1110 | |
1094 | 1111 | |
1095 | 1112 | |
1096 | | - |
| 1113 | + |
1097 | 1114 | |
1098 | 1115 | |
1099 | 1116 | |
1100 | 1117 | |
| 1118 | + |
| 1119 | + |
| 1120 | + |
1101 | 1121 | |
1102 | 1122 | |
1103 | 1123 | |
|
Collapse file
test/parallel/test-fs-stat-bigint.js
Copy file name to clipboardExpand all lines: test/parallel/test-fs-stat-bigint.js+27Lines changed: 27 additions & 0 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
122 | 122 | |
123 | 123 | |
124 | 124 | |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
125 | 152 | |
126 | 153 | |
127 | 154 | |
|
0 commit comments