-
Notifications
You must be signed in to change notification settings - Fork 51
/
sort.perf.ts
132 lines (122 loc) · 2.54 KB
/
sort.perf.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const shuffle = require("knuth-shuffle").knuthShuffle;
import * as R from "ramda";
import { benchmark } from "./report";
import { List } from "immutable";
import * as L from "../../dist/index";
import "../../dist/methods";
import * as Lo from "./list-old/dist/index";
import "./list-old/dist/methods";
let arr: number[];
let l: any;
let imm: any;
function copyArray<A>(arr: A[]): A[] {
const newArray = [];
for (let i = 0; i < arr.length; ++i) {
newArray.push(arr[i]);
}
return newArray;
}
function comparePrimitive(a: number, b: number): -1 | 0 | 1 {
if (a === b) {
return 0;
} else if (a < b) {
return -1;
} else {
return 1;
}
}
function sortUnstable<A extends number | string>(l: L.List<A>): L.List<A> {
return L.from(L.toArray(l).sort(comparePrimitive as any));
}
benchmark(
{
name: "sort, numbers",
description: "Sort a list of numbers",
input: [50, 100, 1000, 5000, 10000, 50000],
before: n => {
arr = [];
for (let i = 0; i < n; ++i) {
arr.push(i);
}
shuffle(arr);
}
},
{
Ramda: {
run: () => {
return R.sort(comparePrimitive, arr).length;
}
},
List: {
before: _ => {
l = L.from(arr);
},
run: () => {
return L.sort(l).length;
}
},
"List, old": {
before: _ => {
l = Lo.from(arr);
},
run: () => {
return Lo.sort(l).length;
}
},
Array: {
run: () => {
return copyArray(arr).sort().length;
}
},
"Array, with comparator": {
run: () => {
return copyArray(arr).sort(comparePrimitive).length;
}
},
"Immutable.js": {
before: _ => {
imm = List(arr);
},
run: () => {
return imm.sort().length;
}
}
}
);
benchmark(
{
name: "sort, stable vs unstable",
description: "Compares the cost of doing a stable search",
input: [50, 100, 1000, 5000, 10000, 50000],
before: n => {
arr = [];
for (let i = 0; i < n; ++i) {
arr.push(i);
}
shuffle(arr);
}
},
{
List: {
before: _ => {
l = L.from(arr);
},
run: () => {
return L.sort(l).length; // Uses unstable sort since numbers
}
},
"List, stable": {
before: _ => {
l = L.from(arr);
},
run: () => {
return L.sortWith(comparePrimitive, l).length; // Uses stable sort
}
},
"Array#sort": {
run: () => {
return copyArray(arr).sort(comparePrimitive).length;
}
}
}
);