mathiasbynens/jsperf.com

Allow code outside the loop, but still inside the tested region

Closed this issue · 1 comments

I needed to generate a sorted array. Going for the simple implementations first, I wanted to compare the speed between splicing in elements at their final places or just pushing them blindly in and then doing one array.sort() at the end.

Here are the two test cases I created. First, the splice-in implementation:

tmp = provideElement();
for (i = 0; i < result.length; i++) {
   if (result[i] >= tmp) {
      break;
   }
}
result.splice(i, tmp);

And then, the 'final sort' implementation:

\\ Looped test code
result.push(provideElement());

\\ The next part isn't looped, but executed once at the end
result.sort();

Uh-uh. I need a way to put the final array.sort() call into the test suite but outside the looped region. I see I can add tearup/teardown sections, but they are not timed.

It seems what I need isn't possible, but it might be a useful feature to add.

jsperf benchmarking is limited to the features in benchmark.js.