-
Notifications
You must be signed in to change notification settings - Fork 6
/
benchmark.js
108 lines (91 loc) · 2.25 KB
/
benchmark.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import sort from './src';
const keys = [];
const keysNotUnique = [];
const results = [];
const resultsUnique = [];
const resultsNotUnique = [];
const keyCount = 100;
const uniqueResultMap = new Array(keyCount);
const iterations = 10000;
let id;
let element;
let i;
let ordered;
function getRandomInt(min, max) {
return Math.floor((Math.random() * ((max - min) + 1)) + min);
}
// generate keys unique keys and unique results.
// use case number 1
for (i = 0; i < keyCount; i++) {
keys.push(i);
results.push({ id: i });
}
// use case number 2 and 3
for (i = 0; i < keyCount; i++) {
id = getRandomInt(0, 80);
element = { id };
keysNotUnique.push(id);
resultsNotUnique.push(element);
if (uniqueResultMap[i]) continue;
resultsUnique.push(element);
uniqueResultMap[i] = true;
}
// order keys randomly
keys.sort(k => Math.random() - 0.5);
console.time('not-repeated');
for (let j = 0; j < iterations; j++) {
try {
ordered = sort(keys, results);
} catch (err) {
// console.log(err);
}
}
console.timeEnd('not-repeated');
console.time('repeated-keys-repeated-values');
for (let j = 0; j < iterations; j++) {
try {
ordered = sort(keysNotUnique, resultsNotUnique);
} catch (err) {
// console.log(err);
}
}
console.timeEnd('repeated-keys-repeated-values');
console.time('repeated-keys-unique-values');
for (let j = 0; j < iterations; j++) {
try {
ordered = sort(keysNotUnique, resultsUnique);
} catch (err) {
// console.log(err);
}
}
console.timeEnd('repeated-keys-unique-values');
const withNulls = keysNotUnique.concat([
getRandomInt(90, 200),
getRandomInt(90, 200),
getRandomInt(90, 200),
]);
withNulls.sort(() => Math.random() - 0.5);
console.time('repeated-keys-unique-values-and-nulls');
for (let j = 0; j < iterations; j++) {
try {
ordered = sort(withNulls, resultsUnique);
} catch (err) {
// console.log(err);
}
}
console.timeEnd('repeated-keys-unique-values-and-nulls');
console.time('empty-keys-with-values');
try {
ordered = sort(null, results);
} catch (err) {
// console.log(err);
}
console.timeEnd('empty-keys-with-values');
console.time('with-keys-empty-values');
try {
ordered = sort(keys, []);
} catch (err) {
// console.log(err);
}
console.timeEnd('with-keys-empty-values');
process.exit(0);