-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.ts
157 lines (144 loc) · 3.87 KB
/
benchmark.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { deepStrictEqual } from 'assert';
import { Suite } from 'benchmark';
import * as cloneDeep from 'clone-deep';
import * as deepmerge from 'deepmerge';
import * as fastDeepEqual from 'fast-deep-equal/es6';
import * as lodash from 'lodash';
import * as markdownTable from 'markdown-table';
import * as ramda from 'ramda';
import * as _ from 'underscore';
import { isDeepStrictEqual } from 'util';
import { clone, isEqual, merge } from './src';
import { mergeTests } from './src/merge/mergeTestDefinitions.spec';
import { isEqualTests } from './src/type-predicates/isEqualTestDefinitions.spec';
interface Benchmark {
name: string;
packages: Record<string, any>;
benchmark: (func: any) => void;
}
const benchmarks: Benchmark[] = [
{
name: `merge`,
packages: {
'@qntm-code/utils.merge': merge,
deepmerge: deepmerge,
},
benchmark: func => {
for (const { target, source } of mergeTests) {
func(target, source);
}
},
},
{
name: `clone`,
packages: {
'@qntm-code/utils.clone': clone,
'lodash.cloneDeep': lodash.cloneDeep,
'clone-deep': cloneDeep,
},
benchmark: func => {
const values = [
new Date(),
['a', 10, new Date()],
{
a: 'a',
b: 10,
c: new Date(),
},
[
['a', 10, new Date()],
{
a: 'a',
b: 10,
c: new Date(),
},
],
{
array: ['a', 10, new Date()],
object: {
a: 'a',
b: 10,
c: new Date(),
},
a: 'a',
b: 10,
c: new Date(),
},
new Map([[1, 2]]),
new Set([1, 2]),
0,
/foo/g,
/foo/,
new RegExp('foo', 'g'),
new RegExp('foo'),
Buffer.from('a'),
Symbol('a'),
new Error('a'),
];
for (const value of values) {
func(value);
}
},
},
{
name: `isEqual`,
packages: {
'@qntm-code/utils.isEqual': isEqual,
'fast-deep-equal': fastDeepEqual,
'underscore.isEqual': _.isEqual,
'lodash.isEqual': lodash.isEqual,
'ramda.equals': ramda.equals,
'util.isDeepStrictEqual': isDeepStrictEqual,
'assert.deepStrictEqual': (a, b) => {
try {
deepStrictEqual(a, b);
return true;
} catch (e) {
return false;
}
},
},
benchmark: func => {
for (const testSuite of isEqualTests) {
for (const test of testSuite.tests) {
if (test.description != 'pseudo array and equivalent array are not equal') {
func(test.a, test.b);
}
}
}
},
},
];
let benchmarkIndex = 0;
function createSuite({ name, packages, benchmark }: Benchmark): void {
const suite = new Suite();
for (const packageName in packages) {
suite.add(packageName, () => benchmark(packages[packageName]));
}
const results: Record<string, number> = {};
console.log(`Benchmarking "${name}"...\n`);
suite
.on('cycle', event => (results[event.target.name] = event.target.hz))
.on('complete', () => {
console.log(
markdownTable([
[`Package`, 'Operations per second'],
...Object.entries(results)
.sort((a, b) => b[1] - a[1])
.map(([name, hz]) => [name, hz.toFixed(0).toString()]),
]),
'\n'
);
benchmarkIndex++;
if (benchmarkIndex < benchmarks.length) {
createSuite(benchmarks[benchmarkIndex]);
}
})
.run({ async: true });
}
createSuite(benchmarks[0]);