-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
110 lines (92 loc) · 3.24 KB
/
main.ts
File metadata and controls
110 lines (92 loc) · 3.24 KB
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
import { configureStore, createSlice } from '@reduxjs/toolkit';
import type { EnhancedStore, PayloadAction, ReducersMapObject } from '@reduxjs/toolkit';
import './style.css';
export const SLICES_COUNT = 500;
export const ACTIONS_PER_SLICE_COUNT = 25;
type SliceState = Record<string, number>;
type SliceAction = PayloadAction<number>;
type SliceReducer = Record<string, (state: SliceState, action: SliceAction) => void>;
const setupStore = () => {
const slices = [];
let createSliceDurationSum = 0;
for (let s = 0; s < SLICES_COUNT; s++) {
const initialState: SliceState = {};
const reducers: SliceReducer = {};
// Each slice state should have value_* properties and a matching amount
// of changed_* actions that modify corresponding value in the state.
for (let a = 0; a < ACTIONS_PER_SLICE_COUNT; a++) {
initialState[`value_${a}`] = 0;
reducers[`changed_${a}`] = (state: SliceState, action: SliceAction) => {
state[`value_${a}`] = action.payload;
};
}
// Creating slice (RTK API)
const start = performance.now();
const slice = createSlice({
name: `slice_${s}`,
initialState,
reducers
});
const end = performance.now();
createSliceDurationSum += end - start;
slices.push(slice);
}
console.log(
performance.measure('create-slices', {
start: 0,
duration: createSliceDurationSum
})
);
// Collecting slice reducers config
const reducer = slices.reduce((acc, { name, reducer }) => {
acc[name] = reducer;
return acc;
}, {} as ReducersMapObject);
// Configuring store (RTK API)
performance.mark('configure-store-start');
const store = configureStore({
reducer
});
performance.mark('configure-store-end');
console.log(
performance.measure('configure-store', {
start: 'configure-store-start',
end: 'configure-store-end',
detail: {
slices: SLICES_COUNT,
actionsPerSlice: ACTIONS_PER_SLICE_COUNT,
actionsTotal: SLICES_COUNT * ACTIONS_PER_SLICE_COUNT,
statePropertiesTotal: SLICES_COUNT + SLICES_COUNT * ACTIONS_PER_SLICE_COUNT
}
})
);
// Listening for changes in store (Redux API)
store.subscribe(() => {
performance.mark('action-end');
console.log(performance.measure('action', 'action-start', 'action-end'));
});
return store;
};
let store: EnhancedStore | undefined;
// Buttons handling
const setupStoreButton = document.getElementById('setupStore') as HTMLButtonElement;
const randomChangeButton = document.getElementById('randomChange') as HTMLButtonElement;
setupStoreButton.textContent += ` (slices: ${SLICES_COUNT}, actions per slice: ${ACTIONS_PER_SLICE_COUNT})`;
setupStoreButton.addEventListener('click', () => {
store = setupStore();
setupStoreButton.disabled = true;
randomChangeButton.disabled = false;
});
randomChangeButton.addEventListener('click', () => {
if (store) {
const sliceName = `slice_${Math.floor(Math.random() * SLICES_COUNT)}`;
const actionName = `changed_${Math.floor(Math.random() * ACTIONS_PER_SLICE_COUNT)}`;
const action = {
type: `${sliceName}/${actionName}`,
payload: Math.random()
};
// Dispatching action (Redux API)
performance.mark('action-start');
store.dispatch(action);
}
});