Run the benchmark:
$ npm i benchmark
# Run the AsyncLocalStorage benchmark
$ node async-local-storage.js ASL
ASL x 15,551 ops/sec ±3.30% (79 runs sampled)
# Run the witness benchmark
$ node async-local-storage.js Witness
Witness x 21,352 ops/sec ±1.27% (87 runs sampled)
There is ~25% perf drop by using AsyncLocalStorage
Micro benchmarking is very difficult and also poorly representative of real world situation.
If you want to know if you can afford to use AsyncLocalStorage then you should try it inside you application and run a benchmark before and after.
For example inside Kuzzle the usage of AsyncLocalStorage results in only 8% performance drop
I didn't get the same result when I run the benchmarks in the same program.
The second benchmark to run is always slower compare to when I run it by itself.
I suspect the garbage collector working to release the memory but I'm not sure.
If you think you know why please contact me by email or find me on Kuzzle Discord (@aschen)
I took a closer look at this and I realized that the reason that the benchmark showed such a disparity was that the control didn’t include a Map allocation and setting. I modified the witness to include the same map allocation and setting and I get much more equal results: https://replit.com/@CaseyChow1/AsyncLocalStorage-Benchmark