I am trying to benchmark an Object's member function using Benchmark.js
. Testing the function is made difficult by several factors:
- Creation of the object is asynchronous (I could mock that part)
- The member function is expensive
- The member function is smart enough to only run once
Let's say it looks like this:
class Something {
constructor(){
// async ops
this.expensiveValue = null;
}
expensiveOperation () {
if (this.expensiveValue === null) {
// Do expensive operation
this.expensiveValue = result; // a non-null value
}
}
}
Now, I want to benchmark expensiveOperation
. But due to its limitations, I also need to "reset" the object each run.
As far as I can tell,benchmark
doesn't support per-run setups. I feel like making the reset part of the run isn't the best practice either, because it pollutes what I'm actually trying to benchmark.
I've looked at Benchmark.setup
, but that only executes per-cycle, not per-run.
Am I missing something? Is there another benchmark
option I can use? Or am I approaching this incorrectly?