Skip to content

Instantly share code, notes, and snippets.

@gaberogan
Last active March 30, 2026 15:10
Show Gist options
  • Select an option

  • Save gaberogan/ba8dc8aaf7f729d854582683377cc07c to your computer and use it in GitHub Desktop.

Select an option

Save gaberogan/ba8dc8aaf7f729d854582683377cc07c to your computer and use it in GitHub Desktop.
Simplified sort key caching - WeakMap per sort cycle instead of pre-computed _sortKey arrays
// sortKey - simplified
// Cache sort values per node during each sort cycle using a WeakMap.
// Values are computed once per node per sort, then discarded when the next sort starts.
// Avoids: _sortKey on data objects, rebuildAllSortKeys, buildSortKey in gridRefresh,
// SORT_KEY_COMPARATOR symbol, rebuildScheduled timeout fallback.
import { getMasterGrid } from 'lib/agGrid'
// PROPS_SORT_COLS still needed - defines sort order per sport
// (same as current implementation, omitted here for brevity)
let sortCache = new WeakMap()
const getSortValues = (node, grid, sortCols) => {
if (sortCache.has(node)) return sortCache.get(node)
const values = sortCols.map(({ colId, transform }) => {
const col = grid.getColumn(colId)
const colDef = col?.getColDef()
if (!colDef) return null
let val
if (typeof colDef.valueGetter === 'function') {
val = colDef.valueGetter({
data: node.data,
node,
colDef,
column: col,
api: grid,
getValue: (f) => node.data[f],
context: grid.getGridOption('context'),
})
} else {
val = node.data[colDef.field ?? colId]
}
return transform ? transform(val ?? null) : val ?? null
})
sortCache.set(node, values)
return values
}
// Usage: in onSortChanged or before each sort trigger
export const clearSortCache = () => {
sortCache = new WeakMap()
}
export const createSortKeyComparator = () => {
return (valueA, valueB, nodeA, nodeB, isDescending) => {
const grid = getMasterGrid()
const sortCols = grid?.getGridOption('context')?.sortCols
if (!sortCols?.length || !grid) return 0
const a = getSortValues(nodeA, grid, sortCols)
const b = getSortValues(nodeB, grid, sortCols)
// return defaultSort(a, b, ...)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment