4 ms·
For the love of god, please do not do this example: for (int i = 0; i<1000; i++) { console.time() // do some expensive work console.timeEnd() }
by sroussey 2y ago
For the love of god, please do not do this example:
for (int i = 0; i<1000; i++) {
console.time()
// do some expensive work
console.timeEnd()
}
Take your timing before and after the loop and divide by the count. Too much jitter otherwise.
d8 and node have many options for benchmarking and if you really care, go command line. JSC is what is behind Bun so you can go that direction as well.
And BTW: console.time et al does a bunch of stuff itself. You will get the JIT looking to optimize it as well in that loop above, lol.
- igouy 2y ago> and divide by the count Which gives an average rather than a time?
- gmokki 2y agoI usually do a var innerCount = 2000; // should run about 2 seconds for (var i=0; i<1000; i++) { var start = currentMillis(); for (var j=0; j<innerCount; j++) { benchmark method(); } best = min(best, (currentMillis() - start) / (double) innerCount); } That way I can both get enough precision form the millisecond resolution and run the whole thing enough times to get the best result without JIT/GC pauses. The result is usually very stable, even when benchmarking calls to database (running locally).