forked from sindresorhus/p-map
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-multiple-pmapskips-performance.mjs
37 lines (31 loc) · 1.3 KB
/
test-multiple-pmapskips-performance.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import test from 'ava';
import inRange from 'in-range';
import timeSpan from 'time-span';
import pMap, {pMapSkip} from './esm/index.js';
function generateSkipPerformanceData(length) {
const data = [];
for (let index = 0; index < length; index++) {
data.push(pMapSkip);
}
return data;
}
test('multiple pMapSkips - algorithmic complexity', async t => {
const testData = [generateSkipPerformanceData(1000), generateSkipPerformanceData(10_000), generateSkipPerformanceData(100_000)];
const testDurationsMS = [];
for (const data of testData) {
const end = timeSpan();
// eslint-disable-next-line no-await-in-loop
await pMap(data, async value => value);
testDurationsMS.push(end());
}
for (let index = 0; index < testDurationsMS.length - 1; index++) {
// Time for 10x more items should take between 9x and 11x more time.
const smallerDuration = testDurationsMS[index];
const longerDuration = testDurationsMS[index + 1];
// The longer test needs to be a little longer and also not 10x more than the
// shorter test. This is not perfect... there is some fluctuation.
// The idea here is to catch a regression that makes `pMapSkip` handling O(n^2)
// on the number of `pMapSkip` items in the input.
t.true(inRange(longerDuration, {start: 1.2 * smallerDuration, end: 15 * smallerDuration}));
}
});