-
Notifications
You must be signed in to change notification settings - Fork 13
/
proxyBenchmark.js
253 lines (205 loc) · 6.29 KB
/
proxyBenchmark.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
To run with comparisons:
$ npm run bench-compare
To run without comparisons:
$ npm run bench
*/
import { JSONPatcherProxy } from '../../src/jsonpatcherproxy.js';
import * as jsonpatch from '../../node_modules/fast-json-patch/index.mjs';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
let includeComparisons = true;
const isNode = (typeof window === 'undefined');
if (isNode) {
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const dom = new JSDOM();
global.window = dom.window;
global.document = dom.window.document;
if (!process.argv.includes("--compare")) {
includeComparisons = false;
}
}
if (typeof Benchmark === 'undefined') {
global.Benchmark = require('benchmark');
global.benchmarkResultsToConsole = require('./../helpers/benchmarkReporter.js').benchmarkResultsToConsole;
global.benchmarkComparisonToFile = require('./../helpers/benchmarkComparisonAdder.js').benchmarkComparisonToFile;
global.benchmarkComparisonToConsole = require('./../helpers/benchmarkComparisonReporter.js').benchmarkComparisonToConsole;
}
const suite = new Benchmark.Suite();
function generateSmallObjectFixture() {
return { name: 'Tesla', speed: 100 };
}
function generateBigObjectFixture(carsSize) {
const obj = {
firstName: 'Albert',
lastName: 'Einstein',
cars: []
};
for (let i = 0; i < carsSize; i++) {
let deep = generateSmallObjectFixture();
obj.cars.push(deep);
for (let j = 0; j < 5; j++) {
deep.temp = generateSmallObjectFixture();
deep = deep.temp;
}
}
return obj;
}
function modifyObj(obj) {
obj.firstName = 'Joachim';
obj.lastName = 'Wester';
obj.cars[0].speed = 123;
obj.cars[0].temp.speed = 456;
}
function reverseString(str) {
return str.split("").reverse().join("");
}
/* ============================= */
{
const suiteName = 'Observe and generate, small object';
if (includeComparisons) {
suite.add(`${suiteName} (noop)`, function() {
const obj = generateBigObjectFixture(1);
modifyObj(obj);
});
}
{
suite.add(`${suiteName} (JSONPatcherProxy)`, function() {
const obj = generateBigObjectFixture(1);
const jsonPatcherProxy = new JSONPatcherProxy(obj);
const observedObj = jsonPatcherProxy.observe(true);
modifyObj(observedObj);
jsonPatcherProxy.generate();
});
}
if (includeComparisons) {
suite.add(`${suiteName} (fast-json-patch)`, function() {
const obj = generateBigObjectFixture(1);
const observer = jsonpatch.observe(obj);
modifyObj(obj);
jsonpatch.generate(observer);
});
}
}
/* ============================= */
{
const suiteName = 'Observe and generate';
if (includeComparisons) {
suite.add(`${suiteName} (noop)`, function() {
const obj = generateBigObjectFixture(100);
modifyObj(obj);
});
}
{
suite.add(`${suiteName} (JSONPatcherProxy)`, function() {
const obj = generateBigObjectFixture(100);
const jsonPatcherProxy = new JSONPatcherProxy(obj);
const observedObj = jsonPatcherProxy.observe(true);
modifyObj(observedObj);
jsonPatcherProxy.generate();
});
}
if (includeComparisons) {
suite.add(`${suiteName} (fast-json-patch)`, function() {
const obj = generateBigObjectFixture(100);
const observer = jsonpatch.observe(obj);
modifyObj(obj);
jsonpatch.generate(observer);
});
}
}
/* ============================= */
{
const suiteName = 'Primitive mutation';
if (includeComparisons) {
const obj = generateBigObjectFixture(100);
suite.add(`${suiteName} (noop)`, function() {
obj.cars[50].name = reverseString(obj.cars[50].name);
});
}
{
const obj = generateBigObjectFixture(100);
const jsonPatcherProxy = new JSONPatcherProxy(obj);
const observedObj = jsonPatcherProxy.observe(true);
suite.add(`${suiteName} (JSONPatcherProxy)`, function() {
observedObj.cars[50].name = reverseString(observedObj.cars[50].name);
jsonPatcherProxy.generate();
});
}
if (includeComparisons) {
const obj = generateBigObjectFixture(100);
const observer = jsonpatch.observe(obj);
suite.add(`${suiteName} (fast-json-patch)`, function() {
obj.cars[50].name = reverseString(obj.cars[50].name);
jsonpatch.generate(observer);
});
}
}
/* ============================= */
{
const suiteName = 'Complex mutation';
if (includeComparisons) {
const obj = generateBigObjectFixture(100);
suite.add(`${suiteName} (noop)`, function() {
const item = obj.cars.shift();
obj.cars.push(item);
});
}
{
const obj = generateBigObjectFixture(100);
const jsonPatcherProxy = new JSONPatcherProxy(obj);
const observedObj = jsonPatcherProxy.observe(true);
suite.add(`${suiteName} (JSONPatcherProxy)`, function() {
const item = observedObj.cars.shift();
observedObj.cars.push(item);
jsonPatcherProxy.generate();
});
}
if (includeComparisons) {
const obj = generateBigObjectFixture(100);
const observer = jsonpatch.observe(obj);
suite.add(`${suiteName} (fast-json-patch)`, function() {
const item = obj.cars.shift();
obj.cars.push(item);
jsonpatch.generate(observer);
});
}
}
/* ============================= */
{
const suiteName = 'Serialization';
if (includeComparisons) {
const obj = generateBigObjectFixture(100);
suite.add(`${suiteName} (noop)`, function() {
JSON.stringify(obj);
});
}
{
const obj = generateBigObjectFixture(100);
const jsonPatcherProxy = new JSONPatcherProxy(obj);
const observedObj = jsonPatcherProxy.observe(true);
suite.add(`${suiteName} (JSONPatcherProxy)`, function() {
JSON.stringify(observedObj);
});
}
if (includeComparisons) {
const obj = generateBigObjectFixture(100);
const observer = jsonpatch.observe(obj);
suite.add(`${suiteName} (fast-json-patch)`, function() {
JSON.stringify(obj);
});
}
}
/* ============================= */
// if we are in the browser with benchmark < 2.1.2
if (typeof benchmarkReporter !== 'undefined') {
benchmarkReporter(suite);
} else {
suite.on('complete', function() {
benchmarkResultsToConsole(suite);
benchmarkComparisonToFile(suite);
benchmarkComparisonToConsole();
});
suite.run();
}