Compare generic helper styles for grouping typed records by a stable key.
type ClickEvent = { kind: 'click'; x: number; y: number; pressure: number }
type ViewEvent = { kind: 'view'; durationMs: number; depth: number }
type PurchaseEvent = { kind: 'purchase'; cents: number; quantity: number }
type UserEvent = ClickEvent | ViewEvent | PurchaseEvent
const events: UserEvent[] = Array.from({ length: 2_000 }, (_, index): UserEvent => {
if (index % 5 === 0) return { kind: 'purchase', cents: 299 + index, quantity: (index % 3) + 1 }
if (index % 2 === 0) return { kind: 'click', x: index % 320, y: index % 180, pressure: (index % 10) / 10 }
return { kind: 'view', durationMs: 120 + (index % 90), depth: index % 7 }
})
const weights: Record<UserEvent['kind'], number> = {
click: 3,
view: 1,
purchase: 11,
}Initializing...
| Test Case | Ops/sec | |
|---|---|---|
| Switch with union narrowing | | ready |
| Record lookup plus common fields | | ready |
| Typed reduce callback | | ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.