@danielgindi/selectbox 2.0.52 → 2.0.53

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.
@@ -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
+ });
package/dist/lib.cjs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @danielgindi/selectbox 2.0.52
2
+ * @danielgindi/selectbox 2.0.53
3
3
  * git://github.com/danielgindi/selectbox.git
4
4
  */
5
5
  'use strict';
@@ -10483,7 +10483,7 @@ class SelectBox {
10483
10483
  p.items = items.slice(0);
10484
10484
  p.itemsChanged = true;
10485
10485
 
10486
- this._updateItemByValueMap();
10486
+ this._refreshItemByValueMap();
10487
10487
 
10488
10488
  if (resetValues) {
10489
10489
  this.setSelectedValues(this.getSelectedValues());
@@ -10514,9 +10514,16 @@ class SelectBox {
10514
10514
  updateItemByValue(value, newItem) {
10515
10515
  const p = this._p;
10516
10516
 
10517
- let existingItem = p.itemByValueMap.get(value);
10518
- if (existingItem)
10519
- Object.assign(existingItem, newItem);
10517
+ let existingItem = this._getItemByValue(value);
10518
+ if (existingItem) {var _p$itemByValueMap;
10519
+ const valueProp = existingItem[p.valueProp];
10520
+ if ((_p$itemByValueMap = p.itemByValueMap) !== null && _p$itemByValueMap !== void 0 && _p$itemByValueMap.has(valueProp))
10521
+ p.itemByValueMap.delete(valueProp);
10522
+
10523
+ Object.assign(existingItem, newItem);
10524
+
10525
+ p.itemByValueMap.set(existingItem[p.valueProp], existingItem);
10526
+ }
10520
10527
 
10521
10528
  if (p.dropList) {
10522
10529
  p.dropList.updateItemByValue(value, newItem);
@@ -11087,7 +11094,7 @@ class SelectBox {
11087
11094
  if (p.dropList)
11088
11095
  p.dropList.setValueProp(prop);
11089
11096
 
11090
- this._updateItemByValueMap();
11097
+ this._refreshItemByValueMap();
11091
11098
 
11092
11099
  return this;
11093
11100
  }
@@ -11273,7 +11280,7 @@ class SelectBox {
11273
11280
 
11274
11281
  selectedValues.push(value);
11275
11282
 
11276
- let item = p.itemByValueMap.get(value);
11283
+ let item = this._getItemByValue(value);
11277
11284
  if (item !== undefined) {
11278
11285
  selectedItems.push(item);
11279
11286
  } else {
@@ -11568,7 +11575,7 @@ class SelectBox {
11568
11575
  * This reduces search time greatly (i.e when checking items), especially when Vue proxies are in place.
11569
11576
  * @private
11570
11577
  */
11571
- _updateItemByValueMap() {
11578
+ _refreshItemByValueMap() {
11572
11579
  const p = this._p;
11573
11580
 
11574
11581
  const itemByValueMap = p.itemByValueMap = new Map();
@@ -11576,6 +11583,52 @@ class SelectBox {
11576
11583
  for (let item of p.items) {
11577
11584
  itemByValueMap.set(item[valueProp], item);
11578
11585
  }
11586
+
11587
+ p.subitemByValueMap = null;
11588
+ }
11589
+
11590
+ /**
11591
+ * @param {*} value
11592
+ * @returns {DropList.ItemBase|undefined}
11593
+ * @private
11594
+ */
11595
+ _getItemByValue(value) {
11596
+ const p = this._p;
11597
+ let item = p.itemByValueMap.get(value);
11598
+ if (item !== undefined)
11599
+ return item;
11600
+
11601
+ if (!p.subitemByValueMap)
11602
+ this._refreshSubitemByValueMap();
11603
+
11604
+ return p.subitemByValueMap.get(value);
11605
+ }
11606
+
11607
+ /** @private */
11608
+ _refreshSubitemByValueMap() {
11609
+ const p = this._p;
11610
+ const subitemByValueMap = p.subitemByValueMap = new Map();
11611
+ this._addSubitemsToValueMap(p.items, subitemByValueMap, p.valueProp);
11612
+ }
11613
+
11614
+ /**
11615
+ * @param {DropList.ItemBase[]} items
11616
+ * @param {Map<*, DropList.ItemBase>} itemByValueMap
11617
+ * @param {string} valueProp
11618
+ * @private
11619
+ */
11620
+ _addSubitemsToValueMap(items, itemByValueMap, valueProp) {
11621
+ for (let item of items) {var _item$_subitems;
11622
+ if (!((_item$_subitems = item._subitems) !== null && _item$_subitems !== void 0 && _item$_subitems.length))
11623
+ continue;
11624
+
11625
+ for (let subitem of item._subitems) {
11626
+ if (!itemByValueMap.has(subitem[valueProp]))
11627
+ itemByValueMap.set(subitem[valueProp], subitem);
11628
+ }
11629
+
11630
+ this._addSubitemsToValueMap(item._subitems, itemByValueMap, valueProp);
11631
+ }
11579
11632
  }
11580
11633
 
11581
11634
  /** @private */
@@ -11960,7 +12013,8 @@ class SelectBox {
11960
12013
  }
11961
12014
  break;
11962
12015
 
11963
- case 'select':{
12016
+ case 'select':
12017
+ case 'subitems:select':{
11964
12018
  if (p.multi) return;
11965
12019
 
11966
12020
  const item = event.item;