mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
57d3b9e2ca
Summary: elementsThatOverlapOffsets was quite inefficient as it was doing a linear scan over all items without an early out for each input offset. The number of items can be large, so we'd want to use binary search here. Before: O(MN), where M is the # offsets and N is the # items After: O(MlogN) As a utility method, elementsThatOverlapOffsets should not be responsible for checking that the offsets are in increasing order 'binary-search' dep added via https://www.internalfb.com/intern/wiki/React_Native/Building_Product_Experiences/Third_Party_Packages_(npm)/ Changelog: [Internal] - Modify VirtualizeUtils.elementsThatOverlapOffsets to use binary search Reviewed By: javache Differential Revision: D36292326 fbshipit-source-id: 5f22eb5533b69e2ebe5c1cb34e4f82605838f0a7
92 lines
2.8 KiB
JavaScript
92 lines
2.8 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @emails oncall+react_native
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
import {elementsThatOverlapOffsets, newRangeCount} from '../VirtualizeUtils';
|
|
|
|
describe('newRangeCount', function () {
|
|
it('handles subset', function () {
|
|
expect(newRangeCount({first: 1, last: 4}, {first: 2, last: 3})).toBe(0);
|
|
});
|
|
it('handles forward disjoint set', function () {
|
|
expect(newRangeCount({first: 1, last: 4}, {first: 6, last: 9})).toBe(4);
|
|
});
|
|
it('handles reverse disjoint set', function () {
|
|
expect(newRangeCount({first: 6, last: 8}, {first: 1, last: 4})).toBe(4);
|
|
});
|
|
it('handles superset', function () {
|
|
expect(newRangeCount({first: 1, last: 4}, {first: 0, last: 5})).toBe(2);
|
|
});
|
|
it('handles end extension', function () {
|
|
expect(newRangeCount({first: 1, last: 4}, {first: 1, last: 8})).toBe(4);
|
|
});
|
|
it('handles front extension', function () {
|
|
expect(newRangeCount({first: 1, last: 4}, {first: 0, last: 4})).toBe(1);
|
|
});
|
|
it('handles forward intersect', function () {
|
|
expect(newRangeCount({first: 1, last: 4}, {first: 3, last: 6})).toBe(2);
|
|
});
|
|
it('handles reverse intersect', function () {
|
|
expect(newRangeCount({first: 3, last: 6}, {first: 1, last: 4})).toBe(2);
|
|
});
|
|
});
|
|
|
|
describe('elementsThatOverlapOffsets', function () {
|
|
it('handles fixed length', function () {
|
|
const offsets = [0, 250, 350, 450];
|
|
function getFrameMetrics(index: number) {
|
|
return {
|
|
length: 100,
|
|
offset: 100 * index,
|
|
};
|
|
}
|
|
expect(
|
|
elementsThatOverlapOffsets(offsets, 100, getFrameMetrics, 1),
|
|
).toEqual([0, 2, 3, 4]);
|
|
});
|
|
it('handles variable length', function () {
|
|
const offsets = [150, 250, 900];
|
|
const frames = [
|
|
{offset: 0, length: 50},
|
|
{offset: 50, length: 200},
|
|
{offset: 250, length: 600},
|
|
{offset: 850, length: 100},
|
|
{offset: 950, length: 150},
|
|
];
|
|
expect(
|
|
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii], 1),
|
|
).toEqual([1, 1, 3]);
|
|
});
|
|
it('handles frame boundaries', function () {
|
|
const offsets = [0, 100, 200, 300];
|
|
function getFrameMetrics(index: number) {
|
|
return {
|
|
length: 100,
|
|
offset: 100 * index,
|
|
};
|
|
}
|
|
expect(
|
|
elementsThatOverlapOffsets(offsets, 100, getFrameMetrics, 1),
|
|
).toEqual([0, 0, 1, 2]);
|
|
});
|
|
it('handles out of bounds', function () {
|
|
const offsets = [-100, 150, 900];
|
|
const frames = [
|
|
{offset: 0, length: 50},
|
|
{offset: 50, length: 150},
|
|
{offset: 250, length: 100},
|
|
];
|
|
expect(
|
|
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii], 1),
|
|
).toEqual([undefined, 1]);
|
|
});
|
|
});
|