Skip to content

Commit 22243e4

Browse files
authored
fix(adapter-commons): Faster sorter (#3495)
1 parent a02febb commit 22243e4

1 file changed

Lines changed: 34 additions & 35 deletions

File tree

  • packages/adapter-commons/src

packages/adapter-commons/src/sort.ts

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
// Sorting algorithm taken from NeDB (https://github.com/louischatriot/nedb)
22
// See https://github.com/louischatriot/nedb/blob/e3f0078499aa1005a59d0c2372e425ab789145c1/lib/model.js#L189
33

4-
export function compareNSB(a: any, b: any) {
5-
if (a < b) {
6-
return -1
7-
}
8-
if (a > b) {
9-
return 1
4+
export function compareNSB(a: number | string | boolean, b: number | string | boolean): 0 | 1 | -1 {
5+
if (a === b) {
6+
return 0
107
}
11-
return 0
8+
9+
return a < b ? -1 : 1
1210
}
1311

14-
export function compareArrays(a: any[], b: any[]) {
12+
export function compareArrays(a: any[], b: any[]): 0 | 1 | -1 {
1513
for (let i = 0, l = Math.min(a.length, b.length); i < l; i++) {
1614
const comparison = compare(a[i], b[i])
1715

@@ -24,48 +22,48 @@ export function compareArrays(a: any[], b: any[]) {
2422
return compareNSB(a.length, b.length)
2523
}
2624

27-
export function compare(a: any, b: any, compareStrings: any = compareNSB): 0 | 1 | -1 {
25+
export function compare(
26+
a: any,
27+
b: any,
28+
compareStrings: (a: any, b: any) => 0 | 1 | -1 = compareNSB
29+
): 0 | 1 | -1 {
2830
if (a === b) {
2931
return 0
3032
}
3133

32-
// undefined
33-
if (a === undefined) {
34+
// null or undefined
35+
if (a == null) {
3436
return -1
3537
}
36-
if (b === undefined) {
38+
if (b == null) {
3739
return 1
3840
}
3941

40-
// null
41-
if (a === null) {
42-
return -1
43-
}
44-
if (b === null) {
45-
return 1
46-
}
42+
// detect typeof once
43+
const typeofA = typeof a
44+
const typeofB = typeof b
4745

4846
// Numbers
49-
if (typeof a === 'number') {
50-
return typeof b === 'number' ? compareNSB(a, b) : -1
47+
if (typeofA === 'number') {
48+
return typeofB === 'number' ? compareNSB(a, b) : -1
5149
}
52-
if (typeof b === 'number') {
50+
if (typeofB === 'number') {
5351
return 1
5452
}
5553

5654
// Strings
57-
if (typeof a === 'string') {
58-
return typeof b === 'string' ? compareStrings(a, b) : -1
55+
if (typeofA === 'string') {
56+
return typeofB === 'string' ? compareStrings(a, b) : -1
5957
}
60-
if (typeof b === 'string') {
58+
if (typeofB === 'string') {
6159
return 1
6260
}
6361

6462
// Booleans
65-
if (typeof a === 'boolean') {
66-
return typeof b === 'boolean' ? compareNSB(a, b) : -1
63+
if (typeofA === 'boolean') {
64+
return typeofB === 'boolean' ? compareNSB(a, b) : -1
6765
}
68-
if (typeof b === 'boolean') {
66+
if (typeofB === 'boolean') {
6967
return 1
7068
}
7169

@@ -100,28 +98,29 @@ export function compare(a: any, b: any, compareStrings: any = compareNSB): 0 | 1
10098
return compareNSB(aKeys.length, bKeys.length)
10199
}
102100

101+
// lodash-y get - probably want to use lodash get instead
102+
const get = (value: any, path: string[]) => path.reduce((value, key) => value[key], value)
103+
103104
// An in-memory sorting function according to the
104105
// $sort special query parameter
105106
export function sorter($sort: { [key: string]: -1 | 1 }) {
106-
const get = (value: any, path: string[]) => path.reduce((value, key) => value[key], value)
107-
108107
const compares = Object.keys($sort).map((key) => {
109108
const direction = $sort[key]
110-
const path = key.split('.')
111109

112-
if (path.length === 1) {
110+
if (!key.includes('.')) {
113111
return (a: any, b: any) => direction * compare(a[key], b[key])
114112
} else {
113+
const path = key.split('.')
115114
return (a: any, b: any) => direction * compare(get(a, path), get(b, path))
116115
}
117116
})
118117

119118
return function (a: any, b: any) {
120119
for (const compare of compares) {
121-
const comparasion = compare(a, b)
120+
const comparison = compare(a, b)
122121

123-
if (comparasion !== 0) {
124-
return comparasion
122+
if (comparison !== 0) {
123+
return comparison
125124
}
126125
}
127126

0 commit comments

Comments
 (0)