@danielgindi/selectbox 2.0.52 → 2.0.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/benchmarks/selectbox-value-lookup.mjs +311 -0
- package/dist/lib.cjs.js +4717 -13467
- package/dist/lib.cjs.js.map +1 -1
- package/dist/lib.cjs.min.js +2 -4
- package/dist/lib.cjs.min.js.map +1 -1
- package/dist/lib.es6.js +4701 -6987
- package/dist/lib.es6.js.map +1 -1
- package/dist/lib.es6.min.js +2 -4
- package/dist/lib.es6.min.js.map +1 -1
- package/dist/lib.umd.js +4743 -13485
- package/dist/lib.umd.js.map +1 -1
- package/dist/lib.umd.min.js +2 -4
- package/dist/lib.umd.min.js.map +1 -1
- package/eslint.config.mjs +1 -1
- package/lib/DropList.js +108 -94
- package/lib/SelectBox.js +77 -14
- package/package.json +13 -18
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
|
|
3
|
+
import { performance } from 'node:perf_hooks';
|
|
4
|
+
|
|
5
|
+
const topLevelCount = 10000;
|
|
6
|
+
const groupsCount = 100;
|
|
7
|
+
const subitemsPerGroup = 50;
|
|
8
|
+
const lookupIterations = 100000;
|
|
9
|
+
const stateIterations = 10000;
|
|
10
|
+
const resetIterations = 100;
|
|
11
|
+
|
|
12
|
+
function createItems() {
|
|
13
|
+
const items = [];
|
|
14
|
+
|
|
15
|
+
for (let i = 0; i < topLevelCount; i++) {
|
|
16
|
+
items.push({ label: `Top ${i}`, value: `top-${i}` });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
for (let groupIndex = 0; groupIndex < groupsCount; groupIndex++) {
|
|
20
|
+
const subitems = [];
|
|
21
|
+
|
|
22
|
+
for (let subitemIndex = 0; subitemIndex < subitemsPerGroup; subitemIndex++) {
|
|
23
|
+
subitems.push({
|
|
24
|
+
label: `Sub ${groupIndex}.${subitemIndex}`,
|
|
25
|
+
value: `sub-${groupIndex}-${subitemIndex}`,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
items.push({
|
|
30
|
+
label: `Group ${groupIndex}`,
|
|
31
|
+
value: `group-${groupIndex}`,
|
|
32
|
+
_subitems: subitems,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return items;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function createSelectBoxState(items, multi = false) {
|
|
40
|
+
const state = {
|
|
41
|
+
_p: {
|
|
42
|
+
items: [],
|
|
43
|
+
itemByValueMap: new Map(),
|
|
44
|
+
subitemByValueMap: null,
|
|
45
|
+
valueProp: 'value',
|
|
46
|
+
multi,
|
|
47
|
+
selectedItems: [],
|
|
48
|
+
selectedValues: [],
|
|
49
|
+
itemsChanged: true,
|
|
50
|
+
selectionChanged: true,
|
|
51
|
+
resortBySelectionNeeded: false,
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
setItems(nextItems, resetValues = true) {
|
|
55
|
+
const p = this._p;
|
|
56
|
+
|
|
57
|
+
p.items = (nextItems ?? []).slice(0);
|
|
58
|
+
p.itemsChanged = true;
|
|
59
|
+
this._updateItemByValueMap();
|
|
60
|
+
|
|
61
|
+
if (resetValues)
|
|
62
|
+
this.setSelectedValues(this.getSelectedValues());
|
|
63
|
+
|
|
64
|
+
return this;
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
setValue(value) {
|
|
68
|
+
const p = this._p;
|
|
69
|
+
if (p.multi)
|
|
70
|
+
return this.setSelectedValues(Array.isArray(value) ? value : value !== undefined ? [value] : []);
|
|
71
|
+
|
|
72
|
+
return this.setSelectedValues(value !== undefined ? [value] : []);
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
setSelectedValues(values) {
|
|
76
|
+
const p = this._p;
|
|
77
|
+
const valueProp = p.valueProp;
|
|
78
|
+
|
|
79
|
+
if (!p.multi)
|
|
80
|
+
values = values.slice(0, 1);
|
|
81
|
+
|
|
82
|
+
const set = new Set();
|
|
83
|
+
const selectedValues = [];
|
|
84
|
+
const selectedItems = [];
|
|
85
|
+
|
|
86
|
+
for (let value of values) {
|
|
87
|
+
if (set.has(value))
|
|
88
|
+
continue;
|
|
89
|
+
set.add(value);
|
|
90
|
+
|
|
91
|
+
selectedValues.push(value);
|
|
92
|
+
|
|
93
|
+
const item = this._getItemByValue(value);
|
|
94
|
+
if (item !== undefined) {
|
|
95
|
+
selectedItems.push(item);
|
|
96
|
+
} else {
|
|
97
|
+
selectedItems.push({ [valueProp]: value });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
p.selectedValues = selectedValues;
|
|
102
|
+
p.selectedItems = selectedItems;
|
|
103
|
+
p.selectionChanged = true;
|
|
104
|
+
p.resortBySelectionNeeded = true;
|
|
105
|
+
|
|
106
|
+
return this;
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
getSelectedValues() {
|
|
110
|
+
return this._p.selectedValues.slice(0);
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
updateItemByValue(value, newItem) {
|
|
114
|
+
const p = this._p;
|
|
115
|
+
const existingItem = this._getItemByValue(value);
|
|
116
|
+
|
|
117
|
+
if (existingItem) {
|
|
118
|
+
const currentValue = existingItem[p.valueProp];
|
|
119
|
+
if (p.itemByValueMap?.has(currentValue))
|
|
120
|
+
p.itemByValueMap.delete(currentValue);
|
|
121
|
+
|
|
122
|
+
Object.assign(existingItem, newItem);
|
|
123
|
+
p.itemByValueMap.set(existingItem[p.valueProp], existingItem);
|
|
124
|
+
p.subitemByValueMap = null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return this;
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
_updateItemByValueMap() {
|
|
131
|
+
const p = this._p;
|
|
132
|
+
const itemByValueMap = p.itemByValueMap = new Map();
|
|
133
|
+
const valueProp = p.valueProp;
|
|
134
|
+
|
|
135
|
+
for (let item of p.items) {
|
|
136
|
+
itemByValueMap.set(item[valueProp], item);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
p.subitemByValueMap = null;
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
_getItemByValue(value) {
|
|
143
|
+
const p = this._p;
|
|
144
|
+
let item = p.itemByValueMap.get(value);
|
|
145
|
+
if (item !== undefined)
|
|
146
|
+
return item;
|
|
147
|
+
|
|
148
|
+
if (!p.subitemByValueMap)
|
|
149
|
+
this._refreshSubitemByValueMap();
|
|
150
|
+
|
|
151
|
+
return p.subitemByValueMap.get(value);
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
_refreshSubitemByValueMap() {
|
|
155
|
+
const p = this._p;
|
|
156
|
+
const subitemByValueMap = p.subitemByValueMap = new Map();
|
|
157
|
+
this._addSubitemsToValueMap(p.items, subitemByValueMap, p.valueProp);
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
_addSubitemsToValueMap(itemsToMap, itemByValueMap, valueProp) {
|
|
161
|
+
for (let item of itemsToMap) {
|
|
162
|
+
if (!item._subitems?.length)
|
|
163
|
+
continue;
|
|
164
|
+
|
|
165
|
+
for (let subitem of item._subitems) {
|
|
166
|
+
if (!itemByValueMap.has(subitem[valueProp]))
|
|
167
|
+
itemByValueMap.set(subitem[valueProp], subitem);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
this._addSubitemsToValueMap(item._subitems, itemByValueMap, valueProp);
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
return state.setItems(items, false);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function assert(condition, message) {
|
|
179
|
+
if (!condition)
|
|
180
|
+
throw new Error(message);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function measure(name, fn) {
|
|
184
|
+
const startedAt = performance.now();
|
|
185
|
+
fn();
|
|
186
|
+
const elapsed = performance.now() - startedAt;
|
|
187
|
+
|
|
188
|
+
console.log(`${name.padEnd(48)} ${elapsed.toFixed(3)}ms`);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function heading(name) {
|
|
192
|
+
console.log(`\n${name}`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const items = createItems();
|
|
196
|
+
const topLevelValue = `top-${topLevelCount - 1}`;
|
|
197
|
+
const otherTopLevelValue = `top-${topLevelCount - 2}`;
|
|
198
|
+
const subitemValue = `sub-${groupsCount - 1}-${subitemsPerGroup - 1}`;
|
|
199
|
+
const otherSubitemValue = `sub-${groupsCount - 1}-${subitemsPerGroup - 2}`;
|
|
200
|
+
|
|
201
|
+
heading('Value map');
|
|
202
|
+
|
|
203
|
+
measure('build top-level value map', () => {
|
|
204
|
+
createSelectBoxState(items);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const lookup = createSelectBoxState(items);
|
|
208
|
+
measure(`${lookupIterations} top-level lookups`, () => {
|
|
209
|
+
for (let i = 0; i < lookupIterations; i++) {
|
|
210
|
+
lookup._getItemByValue(topLevelValue);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
assert(lookup._p.subitemByValueMap === null, 'Top-level lookups should not build the subitem map');
|
|
215
|
+
|
|
216
|
+
measure('first subitem lookup, includes cache build', () => {
|
|
217
|
+
const item = lookup._getItemByValue(subitemValue);
|
|
218
|
+
assert(item?.value === subitemValue, 'Expected subitem to resolve');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
measure(`${lookupIterations} warmed subitem lookups`, () => {
|
|
222
|
+
for (let i = 0; i < lookupIterations; i++) {
|
|
223
|
+
lookup._getItemByValue(subitemValue);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
heading('Single selection');
|
|
228
|
+
|
|
229
|
+
measure(`${stateIterations} setValue(top-level)`, () => {
|
|
230
|
+
const state = createSelectBoxState(items);
|
|
231
|
+
for (let i = 0; i < stateIterations; i++) {
|
|
232
|
+
state.setValue(i % 2 === 0 ? topLevelValue : otherTopLevelValue);
|
|
233
|
+
}
|
|
234
|
+
assert(state._p.subitemByValueMap === null, 'Top-level setValue should not build the subitem map');
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
measure(`${stateIterations} setValue(subitem, cold first)`, () => {
|
|
238
|
+
const state = createSelectBoxState(items);
|
|
239
|
+
for (let i = 0; i < stateIterations; i++) {
|
|
240
|
+
state.setValue(i % 2 === 0 ? subitemValue : otherSubitemValue);
|
|
241
|
+
}
|
|
242
|
+
assert(state._p.selectedItems[0]?.value === otherSubitemValue, 'Expected warmed subitem selection to resolve');
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
measure(`${stateIterations} setValue(unknown)`, () => {
|
|
246
|
+
const state = createSelectBoxState(items);
|
|
247
|
+
for (let i = 0; i < stateIterations; i++) {
|
|
248
|
+
state.setValue(`missing-${i}`);
|
|
249
|
+
}
|
|
250
|
+
assert(state._p.selectedItems[0]?.value === `missing-${stateIterations - 1}`, 'Expected unknown value fallback item');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
heading('Multi selection');
|
|
254
|
+
|
|
255
|
+
measure(`${stateIterations} setSelectedValues(5 top-level)`, () => {
|
|
256
|
+
const state = createSelectBoxState(items, true);
|
|
257
|
+
for (let i = 0; i < stateIterations; i++) {
|
|
258
|
+
state.setSelectedValues(['top-1', 'top-2', 'top-3', 'top-4', 'top-5']);
|
|
259
|
+
}
|
|
260
|
+
assert(state._p.selectedItems.length === 5, 'Expected five selected top-level items');
|
|
261
|
+
assert(state._p.subitemByValueMap === null, 'Top-level multi selection should not build the subitem map');
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
measure(`${stateIterations} setSelectedValues(mixed)`, () => {
|
|
265
|
+
const state = createSelectBoxState(items, true);
|
|
266
|
+
for (let i = 0; i < stateIterations; i++) {
|
|
267
|
+
state.setSelectedValues(['top-1', subitemValue, 'missing-value', otherSubitemValue, 'top-1']);
|
|
268
|
+
}
|
|
269
|
+
assert(state._p.selectedItems.length === 4, 'Expected duplicate selected value to be ignored');
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
heading('Items and updates');
|
|
273
|
+
|
|
274
|
+
measure(`${resetIterations} setItems(reset top-level value)`, () => {
|
|
275
|
+
const state = createSelectBoxState(items);
|
|
276
|
+
state.setValue(topLevelValue);
|
|
277
|
+
|
|
278
|
+
for (let i = 0; i < resetIterations; i++) {
|
|
279
|
+
state.setItems(createItems());
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
assert(state._p.selectedItems[0]?.value === topLevelValue, 'Expected top-level selection after item reset');
|
|
283
|
+
assert(state._p.subitemByValueMap === null, 'Top-level item reset should not build the subitem map');
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
measure(`${resetIterations} setItems(reset subitem value)`, () => {
|
|
287
|
+
const state = createSelectBoxState(items);
|
|
288
|
+
state.setValue(subitemValue);
|
|
289
|
+
|
|
290
|
+
for (let i = 0; i < resetIterations; i++) {
|
|
291
|
+
state.setItems(createItems());
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
assert(state._p.selectedItems[0]?.value === subitemValue, 'Expected subitem selection after item reset');
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
measure(`${stateIterations} updateItemByValue(top-level)`, () => {
|
|
298
|
+
const state = createSelectBoxState(items);
|
|
299
|
+
for (let i = 0; i < stateIterations; i++) {
|
|
300
|
+
state.updateItemByValue('top-0', { label: `Top updated ${i}`, value: 'top-0' });
|
|
301
|
+
}
|
|
302
|
+
assert(state._getItemByValue('top-0')?.label === `Top updated ${stateIterations - 1}`, 'Expected top-level update');
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
measure(`${stateIterations} updateItemByValue(subitem)`, () => {
|
|
306
|
+
const state = createSelectBoxState(items);
|
|
307
|
+
for (let i = 0; i < stateIterations; i++) {
|
|
308
|
+
state.updateItemByValue('sub-0-0', { label: `Sub updated ${i}`, value: 'sub-0-0' });
|
|
309
|
+
}
|
|
310
|
+
assert(state._getItemByValue('sub-0-0')?.label === `Sub updated ${stateIterations - 1}`, 'Expected subitem update');
|
|
311
|
+
});
|