Add reuse and RTKQ benchmark scenarios
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Mark Erikson · committed by Mark Erikson · parent df8629c · Browse files · 1 file·+356 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+356 added−2 removed
- other:+356 added−2 removed
Provenance
- Commit
7266c0f- Tree
db0d0f3- Author
Mark Erikson <mark@isquaredsoftware.com>- Committer
Mark Erikson <mark@isquaredsoftware.com>- Parent
df8629c
1 file changed+356 added−2 removed
modified:perf-testing/immutability-benchmarks.mjs+356 added-2 removed
41 unmodified lines42 otherData: Array.from({length: arraySize}, (_, i) => ({43 id: i,44 name: `name-${i}`,45 isActive: i % 2 === 046- }))46+ })),47+ api: {48+ queries: {},49+ provided: {50+ keys: {}51+ },52+ subscriptions: {}53+ }54 }55 return initialState56 }57 35 unmodified lines93 type: "test/concatArray",94 payload: Array.from({length: 500}, (_, i) => ({id: i, value: index}))95 })96 97+const updateHigh = index => ({98+ type: "test/updateHighIndex",99+ payload: {100+ id:101+ Math.floor(BENCHMARK_CONFIG.arraySize * 0.8) +102+ (index % Math.floor(BENCHMARK_CONFIG.arraySize * 0.2)),103+ value: index,104+ nestedData: index105+ }106+})107+const updateMultiple = index => ({108+ type: "test/updateMultiple",109+ payload: Array.from({length: BENCHMARK_CONFIG.multiUpdateCount}, (_, i) => ({110+ id: (index + i) % BENCHMARK_CONFIG.arraySize,111+ value: index + i,112+ nestedData: index + i113+ }))114+})115+const removeHigh = index => ({116+ type: "test/removeHighIndex",117+ payload:118+ Math.floor(BENCHMARK_CONFIG.arraySize * 0.8) +119+ (index % Math.floor(BENCHMARK_CONFIG.arraySize * 0.2))120+})121+122+const sortByIdReverse = () => ({123+ type: "test/sortByIdReverse"124+})125+126+const reverseArray = () => ({127+ type: "test/reverseArray"128+})129+130+// RTKQ-style action creators131+const rtkqPending = index => ({132+ type: "rtkq/pending",133+ payload: {134+ cacheKey: `some("test-${index}-")`,135+ requestId: `req-${index}`,136+ id: `test-${index}-`137+ }138+})139+140+const rtkqResolved = index => ({141+ type: "rtkq/resolved",142+ payload: {143+ cacheKey: `some("test-${index}-")`,144+ requestId: `req-${index}`,145+ id: `test-${index}-`,146+ data: `test-${index}-1`147+ }148+})149+150 const actions = {151 add,152 remove,153 filter,154 update,95- concat155+ concat,156+ // dash-named fields to improve readability in benchmark results157+ "update-high": updateHigh,158+ "update-multiple": updateMultiple,159+ "remove-high": removeHigh,160+ "sortById-reverse": sortByIdReverse,161+ "reverse-array": reverseArray162 }163 164 const immerProducers = {165 // immer5: produce5,39 unmodified lines205 mutativeCompat: noop,206 structura: noop,207 limu: noop208 }209+210+// RTKQ-style separate reducer functions (simulating separate RTK slices)211+const updateQueries = (queries, action) => {212+ switch (action.type) {213+ case "rtkq/pending":214+ return {215+ ...queries,216+ [action.payload.cacheKey]: {217+ id: action.payload.id,218+ status: "pending",219+ data: undefined220+ }221+ }222+ case "rtkq/resolved":223+ return {224+ ...queries,225+ [action.payload.cacheKey]: {226+ ...queries[action.payload.cacheKey],227+ status: "fulfilled",228+ data: action.payload.data229+ }230+ }231+ default:232+ return queries233+ }234 }235 236+const updateProvided = (provided, action) => {237+ switch (action.type) {238+ case "rtkq/pending":239+ case "rtkq/resolved":240+ return {241+ ...provided,242+ keys: {243+ ...provided.keys,244+ [action.payload.cacheKey]: {}245+ }246+ }247+ default:248+ return provided249+ }250+}251+252+const updateSubscriptions = (subscriptions, action) => {253+ switch (action.type) {254+ case "rtkq/pending":255+ return {256+ ...subscriptions,257+ [action.payload.cacheKey]: {258+ [action.payload.requestId]: {259+ pollingInterval: 0,260+ skipPollingIfUnfocused: false261+ }262+ }263+ }264+ case "rtkq/resolved":265+ return subscriptions // No change on resolved266+ default:267+ return subscriptions268+ }269+}270+271 const vanillaReducer = (state = createInitialState(), action) => {272 switch (action.type) {273 case "test/addItem":274 return {41 unmodified lines316 ...state,317 largeArray: newArray318 }319 }320+ case "test/updateHighIndex": {321+ return {322+ ...state,323+ largeArray: state.largeArray.map(item =>324+ item.id === action.payload.id325+ ? {326+ ...item,327+ value: action.payload.value,328+ nested: {...item.nested, data: action.payload.nestedData}329+ }330+ : item331+ )332+ }333+ }334+ case "test/updateMultiple": {335+ const updates = new Map(action.payload.map(p => [p.id, p]))336+ return {337+ ...state,338+ largeArray: state.largeArray.map(item => {339+ const update = updates.get(item.id)340+ return update341+ ? {342+ ...item,343+ value: update.value,344+ nested: {...item.nested, data: update.nestedData}345+ }346+ : item347+ })348+ }349+ }350+ case "test/removeHighIndex": {351+ const newArray = state.largeArray.slice()352+ const indexToRemove = newArray.findIndex(353+ item => item.id === action.payload354+ )355+ if (indexToRemove !== -1) {356+ newArray.splice(indexToRemove, 1)357+ }358+ return {359+ ...state,360+ largeArray: newArray361+ }362+ }363+ case "test/sortByIdReverse": {364+ const newArray = state.largeArray.slice()365+ newArray.sort((a, b) => b.id - a.id) // Sort by ID in reverse order366+ return {367+ ...state,368+ largeArray: newArray369+ }370+ }371+ case "test/reverseArray": {372+ const newArray = state.largeArray.slice()373+ newArray.reverse()374+ return {375+ ...state,376+ largeArray: newArray377+ }378+ }379+ case "rtkq/pending":380+ case "rtkq/resolved": {381+ // Simulate separate RTK slice reducers with combined reducer pattern382+ return {383+ ...state,384+ api: {385+ queries: updateQueries(state.api.queries, action),386+ provided: updateProvided(state.api.provided, action),387+ subscriptions: updateSubscriptions(state.api.subscriptions, action)388+ }389+ }390+ }391 default:392 return state393 }394 }30 unmodified lines425 newArray.length = length426 draft.largeArray = newArray427 break428 }429+ case "test/updateHighIndex": {430+ const item = draft.largeArray.find(431+ item => item.id === action.payload.id432+ )433+ if (item) {434+ item.value = action.payload.value435+ item.nested.data = action.payload.nestedData436+ }437+ break438+ }439+ case "test/updateMultiple": {440+ action.payload.forEach(update => {441+ const item = draft.largeArray.find(item => item.id === update.id)442+ if (item) {443+ item.value = update.value444+ item.nested.data = update.nestedData445+ }446+ })447+ break448+ }449+ case "test/removeHighIndex": {450+ const indexToRemove = draft.largeArray.findIndex(451+ item => item.id === action.payload452+ )453+ if (indexToRemove !== -1) {454+ draft.largeArray.splice(indexToRemove, 1)455+ }456+ break457+ }458+ case "test/sortByIdReverse": {459+ draft.largeArray.sort((a, b) => b.id - a.id)460+ break461+ }462+ case "test/reverseArray": {463+ draft.largeArray.reverse()464+ break465+ }466+ case "rtkq/pending": {467+ // Simulate separate RTK slice reducers with combined reducer pattern468+ const cacheKey = action.payload.cacheKey469+ draft.api.queries[cacheKey] = {470+ id: action.payload.id,471+ status: "pending",472+ data: undefined473+ }474+ draft.api.provided.keys[cacheKey] = {}475+ draft.api.subscriptions[cacheKey] = {476+ [action.payload.requestId]: {477+ pollingInterval: 0,478+ skipPollingIfUnfocused: false479+ }480+ }481+ break482+ }483+ case "rtkq/resolved": {484+ const cacheKey = action.payload.cacheKey485+ draft.api.queries[cacheKey].status = "fulfilled"486+ draft.api.queries[cacheKey].data = action.payload.data487+ // provided and subscriptions don't change on resolved488+ break489+ }490 }491 })492 493 return immerReducer41 unmodified lines535 action: [action]536 })537 })538 }539+540+ // State reuse benchmarks (tests performance on frozen/evolved state)541+ const reuseActions = ["update", "update-high", "remove", "remove-high"]542+ for (const action of reuseActions) {543+ summary(function() {544+ bench(`$action-reuse: $version (freeze: $freeze)`, function*(args) {545+ const version = args.get("version")546+ const freeze = args.get("freeze")547+ const action = args.get("action")548+549+ function benchMethod() {550+ setAutoFreezes[version](freeze)551+ setStrictIteration[version](false)552+553+ let currentState = createInitialState()554+555+ // Perform multiple operations on the same evolving state556+ for (let i = 0; i < BENCHMARK_CONFIG.reuseStateIterations; i++) {557+ currentState = reducers[version](currentState, actions[action](i))558+ }559+ setAutoFreezes[version](false)560+ }561+562+ yield benchMethod563+ }).args({564+ version: Object.keys(reducers),565+ freeze,566+ action: [action]567+ })568+ })569+ }570+571+ // Mixed operations sequence benchmark572+ summary(function() {573+ bench(`mixed-sequence: $version (freeze: $freeze)`, function*(args) {574+ const version = args.get("version")575+ const freeze = args.get("freeze")576+577+ function benchMethod() {578+ setAutoFreezes[version](freeze)579+ setStrictIteration[version](false)580+581+ let state = createInitialState()582+583+ // Perform a sequence of different operations (typical workflow)584+ state = reducers[version](state, actions.add(1))585+ state = reducers[version](state, actions.update(getValidId()))586+ state = reducers[version](state, actions["update-high"](2))587+ state = reducers[version](state, actions["update-multiple"](3))588+ state = reducers[version](state, actions.remove(getValidIndex()))589+590+ setAutoFreezes[version](false)591+ }592+593+ yield benchMethod594+ }).args({595+ version: Object.keys(reducers),596+ freeze597+ })598+ })599+600+ // RTKQ-style benchmark - executes multiple reducer calls in sequence601+ summary(function() {602+ bench(`rtkq-sequence: $version (freeze: $freeze)`, function*(args) {603+ const version = args.get("version")604+ const freeze = args.get("freeze")605+606+ function benchMethod() {607+ setAutoFreezes[version](freeze)608+ setStrictIteration[version](false)609+610+ let state = createInitialState()611+ // Use smaller array size for RTKQ benchmark due to exponential scaling612+ // 100 items = ~15ms, 200 items = ~32ms, so 10000 would be impractical613+ const arraySize = 100614+615+ // Phase 1: Execute all pending actions616+ for (let i = 0; i < arraySize; i++) {617+ state = reducers[version](state, rtkqPending(i))618+ }619+620+ // Phase 2: Execute all resolved actions621+ for (let i = 0; i < arraySize; i++) {622+ state = reducers[version](state, rtkqResolved(i))623+ }624+625+ setAutoFreezes[version](false)626+ }627+628+ yield benchMethod629+ }).args({630+ version: Object.keys(reducers),631+ freeze632+ })633+ })634 }635+}636 637 async function main() {638 createBenchmarks()639 await run()4 unmodified lines