perf: measure sidebar row geometry once before sorting delete anchors

This commit is contained in:
m4air
2026-09-07 19:50:03 -07:00
parent d0506bf5de
commit 41bfeb0dca
2 changed files with 26 additions and 3 deletions
@@ -171,9 +171,10 @@ export function preserveDeleteSiblingPosition(scope: HTMLElement | null): () =>
if (!(sidebar instanceof HTMLElement) || !(row instanceof HTMLElement)) {
return () => {}
}
const rows = Array.from(
sidebar.querySelectorAll<HTMLElement>('[data-worktree-virtual-row]')
).sort((a, b) => a.getBoundingClientRect().top - b.getBoundingClientRect().top)
const rows = Array.from(sidebar.querySelectorAll<HTMLElement>('[data-worktree-virtual-row]'))
.map((element) => ({ element, top: element.getBoundingClientRect().top }))
.sort((a, b) => a.top - b.top)
.map(({ element }) => element)
const rowIndex = rows.indexOf(row)
const anchorRow = rows[rowIndex + 1] ?? rows[rowIndex - 1] ?? null
const anchorKey = anchorRow?.getAttribute('data-worktree-virtual-row-key')
@@ -0,0 +1,22 @@
// @vitest-environment happy-dom
import { expect, it } from 'vitest'
import { preserveDeleteSiblingPosition } from './worktree-context-menu-policy'
it('measures each mounted sidebar row once when choosing a delete-position anchor', () => {
const sidebar = document.createElement('div')
sidebar.setAttribute('data-worktree-sidebar', '')
let measurements = 0
const rows = Array.from({ length: 200 }, (_, index) => {
const row = document.createElement('div')
row.setAttribute('data-worktree-virtual-row', '')
row.setAttribute('data-worktree-virtual-row-key', String(index))
row.getBoundingClientRect = () => {
measurements += 1
return { top: (index * 73) % 200 } as DOMRect
}
sidebar.append(row)
return row
})
expect(typeof preserveDeleteSiblingPosition(rows[100])).toBe('function')
expect(measurements).toBeLessThanOrEqual(201)
})