You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -131,7 +135,10 @@ A `Suite` manages and executes benchmark functions. It provides two methods: `ad
131
135
*`useWorkers` {boolean} Whether to run benchmarks in worker threads. **Default:**`false`.
132
136
*`plugins` {Array} Array of plugin instances to use.
133
137
*`repeatSuite` {number} Number of times to repeat each benchmark. Automatically set to `30` when `ttest: true`. **Default:**`1`.
138
+
*`plugins` {Array} Array of plugin instances to use. **Default:**`[V8NeverOptimizePlugin]`.
134
139
*`minSamples` {number} Minimum number of samples per round for all benchmarks in the suite. Can be overridden per benchmark. **Default:**`10` samples.
140
+
*`detectDeadCodeElimination` {boolean} Enable dead code elimination detection. When enabled, default plugins are disabled to allow V8 optimizations. **Default:**`false`.
141
+
*`dceThreshold` {number} Threshold multiplier for DCE detection. Benchmarks faster than baseline × threshold will trigger warnings. **Default:**`10`.
135
142
136
143
If no `reporter` is provided, results are printed to the console.
137
144
@@ -179,6 +186,78 @@ Using delete property x 5,853,505 ops/sec (10 runs sampled) min..max=(169ns ...
179
186
180
187
Runs all added benchmarks and returns the results.
181
188
189
+
## Dead Code Elimination Detection
190
+
191
+
**bench-node** includes optional detection of dead code elimination (DCE) to help identify benchmarks that may be producing inaccurate results. When the JIT compiler optimizes away your benchmark code, it can run nearly as fast as an empty function, leading to misleading performance measurements.
192
+
193
+
**Important:** DCE detection is **opt-in**. When enabled, the `V8NeverOptimizePlugin` is automatically disabled to allow V8 optimizations to occur naturally. This helps catch benchmarks that would be optimized away in real-world scenarios.
194
+
195
+
### How It Works
196
+
197
+
When enabled, bench-node measures a baseline (empty function) performance before running your benchmarks. After each benchmark completes, it compares the timing against this baseline. If a benchmark runs suspiciously fast (less than 10× slower than the baseline by default), a warning is emitted.
198
+
199
+
### Example Warning Output
200
+
201
+
```
202
+
⚠️ Dead Code Elimination Warnings:
203
+
The following benchmarks may have been optimized away by the JIT compiler:
204
+
205
+
• array creation
206
+
Benchmark: 3.98ns/iter
207
+
Baseline: 0.77ns/iter
208
+
Ratio: 5.18x of baseline
209
+
Suggestion: Ensure the result is used or assign to a variable
210
+
211
+
ℹ️ These benchmarks are running nearly as fast as an empty function,
212
+
which suggests the JIT may have eliminated the actual work.
// Enable DCE detection with custom threshold (default is 10x)
226
+
conststrictSuite=newSuite({
227
+
detectDeadCodeElimination:true,
228
+
dceThreshold:20// Only warn if < 20x slower than baseline
229
+
});
230
+
231
+
// Use both DCE detection AND prevent optimization
232
+
// (helpful for educational purposes to see warnings even when using %NeverOptimizeFunction)
233
+
consteducationalSuite=newSuite({
234
+
plugins: [newV8NeverOptimizePlugin()],
235
+
detectDeadCodeElimination:true
236
+
});
237
+
```
238
+
239
+
### When DCE Warnings Appear
240
+
241
+
Common scenarios that trigger warnings:
242
+
243
+
```js
244
+
// ❌ Result not used - will be optimized away
245
+
suite.add('computation', () => {
246
+
constresult=Math.sqrt(144);
247
+
// result is never used
248
+
});
249
+
250
+
// ✅ Result is used - less likely to be optimized
251
+
suite.add('computation', () => {
252
+
constresult=Math.sqrt(144);
253
+
if (result !==12) thrownewError('Unexpected');
254
+
});
255
+
```
256
+
257
+
**Note:** DCE detection only works in `'ops'` benchmark mode and when not using worker threads. It is automatically disabled for `'time'` mode and worker-based benchmarks.
258
+
259
+
See [examples/dce-detection/](./examples/dce-detection/) for more examples.
260
+
182
261
## Plugins
183
262
184
263
Plugins extend the functionality of the benchmark module.
0 commit comments