Prevent dead code elimination in benchmarks
masterJavaScript JIT compilers can detect and eliminate code that has no observable side effects. To ensure your benchmarked code is actually executed and not optimized away, use the do_not_optimize(value) function. This function emits code that forces the engine to treat the value as having observable side effects.
import { do_not_optimize } from 'mitata';
bench(function* () {
// ❌ Bad: jit can see that function has zero side-effects
yield () => new Array(0);
// will get optimized to:
/*
yield () => {};
*/
// ✅ Good: do_not_optimize(value) emits code that causes side-effects
yield () => do_not_optimize(new Array(0));
});