-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
122 lines (108 loc) · 2.38 KB
/
index.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
import 'babel-polyfill';
import fs from 'fs';
import Benchmark from 'benchmark';
import React from 'react';
import {Provider} from 'react-redux';
import {renderToString} from 'react/dist/react.min';
import {getDataDependencies} from 'relate-js';
import createStore from './src/create-store';
import RootComponent from './src/components';
function getComp () {
const store = createStore();
return (
<Provider store={store}>
<RootComponent />
</Provider>
);
}
function getData (query) {
return {
data: {
pages: [
{
_id: 'a',
title: 'A'
},
{
_id: 'b',
title: 'B'
}
],
page: {
_id: 'c',
title: 'C',
date: 123
}
},
errors: null
};
}
async function checkResult () {
const store = createStore();
const component = (
<Provider store={store}>
<RootComponent />
</Provider>
);
await getDataDependencies(component, getData);
const result = renderToString(component);
fs.writeFile('./build/result.html', result, (err) => {
if (err) {
return console.log(err);
}
console.log('Result saved!');
console.log(store.getState());
});
}
async function renderRoute (component) {
await getDataDependencies(component, getData);
renderToString(component);
}
async function renderRouteRelate (component) {
await getDataDependencies(component, getData);
}
const suite = new Benchmark.Suite;
function output (bench) {
console.log(bench.name);
console.log('Mean:', bench.stats.mean * 1000, 'ms');
console.log('Std dev:', bench.stats.deviation * 1000, 'ms');
}
suite
.add('Relate + React', {
defer: true,
fn: (deferred) => {
renderRoute(getComp())
.then(() => {
deferred.resolve();
})
.catch((error) => {
console.log('error', error);
deferred.reject();
});
}
})
.add('Relate', {
defer: true,
fn: (deferred) => {
renderRouteRelate(getComp())
.then(() => {
deferred.resolve();
})
.catch((error) => {
console.log('error', error);
deferred.reject();
});
}
})
.add('React', {
fn: () => {
renderToString(getComp());
}
})
.on('complete', function () {
output(this[0]);
output(this[1]);
output(this[2]);
checkResult();
})
.run();