Skip to content

Commit 353d56b

Browse files
authored
refactor: minimize virtualizer item create rounds (#5713)
1 parent 0512cb6 commit 353d56b

4 files changed

Lines changed: 92 additions & 6 deletions

File tree

‎packages/combo-box/test/lazy-loading.test.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ describe('lazy loading', () => {
813813
it('should render selectedItem as selected when setting it while loading more items', async () => {
814814
const alreadyLoadedItem = `item ${comboBox.pageSize - 1}`;
815815

816-
scrollToIndex(comboBox, comboBox.pageSize + 1);
816+
scrollToIndex(comboBox, comboBox.pageSize - 1);
817817
await nextFrame();
818818
expect(comboBox.loading).to.be.true;
819819

@@ -865,7 +865,7 @@ describe('lazy loading', () => {
865865
label: `label ${comboBox.pageSize - 1}`,
866866
};
867867

868-
scrollToIndex(comboBox, comboBox.pageSize + 1);
868+
scrollToIndex(comboBox, comboBox.pageSize - 1);
869869
await nextFrame();
870870
expect(comboBox.loading).to.be.true;
871871

‎packages/component-base/src/virtualizer-iron-list-adapter.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,29 @@ export class IronListAdapter {
563563
);
564564
}
565565

566+
/**
567+
* Increases the pool size.
568+
* @override
569+
*/
570+
_increasePoolIfNeeded(count) {
571+
if (this._physicalCount > 2 && count) {
572+
// The iron-list logic has already created some physical items and
573+
// has decided to create more. Since each item creation round is
574+
// expensive, let's try to create the remaining items in one go.
575+
576+
// Calculate the total item count that would be needed to fill the viewport
577+
// plus the buffer assuming rest of the items to be of the average size
578+
// of the items already created.
579+
const totalItemCount = Math.ceil(this._optPhysicalSize / this._physicalAverage);
580+
const missingItemCount = totalItemCount - this._physicalCount;
581+
// Create the remaining items in one go. Use a maximum of 100 items
582+
// as a safety measure.
583+
super._increasePoolIfNeeded(Math.max(count, Math.min(100, missingItemCount)));
584+
} else {
585+
super._increasePoolIfNeeded(count);
586+
}
587+
}
588+
566589
/**
567590
* @returns {Number|undefined} - The browser's default font-size in pixels
568591
* @private

‎packages/component-base/test/virtualizer-item-height.test.js‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { aTimeout, fixtureSync } from '@vaadin/testing-helpers';
2+
import { aTimeout, fixtureSync, nextFrame } from '@vaadin/testing-helpers';
3+
import sinon from 'sinon';
34
import { Virtualizer } from '../src/virtualizer.js';
45

56
describe('virtualizer - item height', () => {
@@ -181,3 +182,62 @@ describe('virtualizer - item height - sub-pixel', () => {
181182
expect(containerHeight).to.equal(Math.ceil(ITEM_HEIGHT));
182183
});
183184
});
185+
186+
describe('virtualizer - item height - initial render', () => {
187+
let virtualizer, elementsContainer, itemHeight, createElements;
188+
189+
beforeEach(() => {
190+
const scrollTarget = fixtureSync(`
191+
<div style="height: 300px;">
192+
<div class="container"></div>
193+
</div>
194+
`);
195+
const scrollContainer = scrollTarget.firstElementChild;
196+
elementsContainer = scrollContainer;
197+
198+
createElements = sinon.spy((count) => Array.from({ length: count }, () => document.createElement('div')));
199+
200+
virtualizer = new Virtualizer({
201+
createElements,
202+
updateElement: (el, index) => {
203+
el.style.width = '100%';
204+
el.style.height = itemHeight;
205+
el.textContent = `Item ${index}`;
206+
},
207+
scrollTarget,
208+
scrollContainer,
209+
});
210+
});
211+
212+
describe('large item height', () => {
213+
beforeEach(async () => {
214+
itemHeight = '100px';
215+
virtualizer.size = 100;
216+
await nextFrame();
217+
});
218+
219+
it('should have the expected amount of physical elements', () => {
220+
expect(elementsContainer.childElementCount).to.equal(5);
221+
});
222+
223+
it('should have created the items in the expected amount of batches', () => {
224+
expect(createElements.callCount).to.equal(2);
225+
});
226+
});
227+
228+
describe('small item height', () => {
229+
beforeEach(async () => {
230+
itemHeight = '20px';
231+
virtualizer.size = 100;
232+
await nextFrame();
233+
});
234+
235+
it('should have the expected amount of physical elements', () => {
236+
expect(elementsContainer.childElementCount).to.equal(20);
237+
});
238+
239+
it('should have created the items in the expected amount of batches', () => {
240+
expect(createElements.callCount).to.equal(2);
241+
});
242+
});
243+
});

‎packages/grid/test/filtering.test.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fire, fixtureSync, oneEvent } from '@vaadin/testing-helpers';
2+
import { fire, fixtureSync, nextFrame, oneEvent } from '@vaadin/testing-helpers';
33
import sinon from 'sinon';
44
import '../vaadin-grid.js';
55
import '../vaadin-grid-filter.js';
@@ -192,7 +192,7 @@ describe('filtering', () => {
192192
expect(spy.calledOnce).to.be.true;
193193
});
194194

195-
it('should display all filtered items', () => {
195+
it('should display all filtered items', async () => {
196196
flushFilters(grid);
197197
grid._filters[0].value = '';
198198
grid._filters[1].value = '';
@@ -206,8 +206,11 @@ describe('filtering', () => {
206206

207207
grid._filters[0].value = '99';
208208
flushFilters(grid);
209+
210+
grid.allRowsVisible = true;
209211
flushGrid(grid);
210-
expect(grid.$.items.querySelectorAll('tr:not([hidden])')).to.have.length(18);
212+
await nextFrame();
213+
expect(grid.$.items.querySelectorAll('tr:not([hidden])')).to.have.length(19);
211214
});
212215

213216
it('should not overflow filter text field', () => {

0 commit comments

Comments
 (0)