@@ -107,61 +107,48 @@ async function runPerformanceObserverBenchmarks () {
107107 phase2Cache . set ( testData [ i ] . key , testData [ i ] . value ) ;
108108 }
109109
110- // Deterministic mixed workload without Math.random in the loop
111- const choice = new Uint8Array ( 5000 ) ;
112- const indices = new Uint32Array ( 5000 ) ;
113- let a = 1103515245 , c = 12345 , m = 2 ** 31 ;
114- let seed = 42 ;
115- for ( let i = 0 ; i < 5000 ; i ++ ) {
116- seed = ( a * seed + c ) % m ;
117- const r = seed >>> 0 ;
118- choice [ i ] = r % 100 ; // 0..99
119- indices [ i ] = r % testData . length ;
110+ // Deterministic mixed workload that exercises the entire cache without conditionals
111+ const phase2Iterations = 10000 ;
112+ const getIndices = new Uint32Array ( phase2Iterations ) ;
113+ const setIndices = new Uint32Array ( phase2Iterations ) ;
114+ const hasIndices = new Uint32Array ( phase2Iterations ) ;
115+ const deleteIndices = new Uint32Array ( phase2Iterations ) ;
116+
117+ for ( let i = 0 ; i < phase2Iterations ; i ++ ) {
118+ const idx = i % cacheSize ;
119+ getIndices [ i ] = idx ;
120+ setIndices [ i ] = idx ;
121+ hasIndices [ i ] = idx ;
122+ deleteIndices [ i ] = idx ;
120123 }
121124
122125 let mixedGetIndex = 0 ;
123126 await timer . timeFunction ( "lru.get (mixed workload)" , ( ) => {
124- const i = mixedGetIndex % choice . length ;
125- const pick = choice [ i ] ;
126- if ( pick < 60 ) {
127- const idx = indices [ i ] ;
128- phase2Cache . get ( testData [ idx ] . key ) ;
129- }
127+ const idx = getIndices [ mixedGetIndex % phase2Iterations ] ;
128+ phase2Cache . get ( testData [ idx ] . key ) ;
130129 mixedGetIndex ++ ;
131- } , 10000 ) ;
130+ } , phase2Iterations ) ;
132131
133132 let mixedSetIndex = 0 ;
134133 await timer . timeFunction ( "lru.set (mixed workload)" , ( ) => {
135- const i = mixedSetIndex % choice . length ;
136- const pick = choice [ i ] ;
137- if ( pick >= 60 && pick < 80 ) {
138- const idx = indices [ i ] ;
139- phase2Cache . set ( testData [ idx ] . key , testData [ idx ] . value ) ;
140- }
134+ const idx = setIndices [ mixedSetIndex % phase2Iterations ] ;
135+ phase2Cache . set ( testData [ idx ] . key , testData [ idx ] . value ) ;
141136 mixedSetIndex ++ ;
142- } , 10000 ) ;
137+ } , phase2Iterations ) ;
143138
144139 let mixedHasIndex = 0 ;
145140 await timer . timeFunction ( "lru.has (mixed workload)" , ( ) => {
146- const i = mixedHasIndex % choice . length ;
147- const pick = choice [ i ] ;
148- if ( pick >= 80 && pick < 95 ) {
149- const idx = indices [ i ] ;
150- phase2Cache . has ( testData [ idx ] . key ) ;
151- }
141+ const idx = hasIndices [ mixedHasIndex % phase2Iterations ] ;
142+ phase2Cache . has ( testData [ idx ] . key ) ;
152143 mixedHasIndex ++ ;
153- } , 10000 ) ;
144+ } , phase2Iterations ) ;
154145
155146 let mixedDeleteIndex = 0 ;
156147 await timer . timeFunction ( "lru.delete (mixed workload)" , ( ) => {
157- const i = mixedDeleteIndex % choice . length ;
158- const pick = choice [ i ] ;
159- if ( pick >= 95 ) {
160- const idx = indices [ i ] ;
161- phase2Cache . delete ( testData [ idx ] . key ) ;
162- }
148+ const idx = deleteIndices [ mixedDeleteIndex % phase2Iterations ] ;
149+ phase2Cache . delete ( testData [ idx ] . key ) ;
163150 mixedDeleteIndex ++ ;
164- } , 10000 ) ;
151+ } , phase2Iterations ) ;
165152
166153 // Phase 3: Cache eviction stress test
167154 console . log ( "Phase 3: Cache eviction stress test" ) ;
@@ -229,59 +216,45 @@ async function runCustomTimerBenchmarks () {
229216 phase2Cache . set ( testData [ i ] . key , testData [ i ] . value ) ;
230217 }
231218
232- // Deterministic mixed workload without Math.random in the loop
233- const choice = new Uint8Array ( 5000 ) ;
234- const indices = new Uint32Array ( 5000 ) ;
235- let a = 1103515245 , c = 12345 , m = 2 ** 31 ;
236- let seed = 42 ;
237- for ( let i = 0 ; i < 5000 ; i ++ ) {
238- seed = ( a * seed + c ) % m ;
239- const r = seed >>> 0 ;
240- choice [ i ] = r % 100 ; // 0..99
241- indices [ i ] = r % testData . length ;
219+ // Deterministic mixed workload that exercises the entire cache without conditionals
220+ const getIndices = new Uint32Array ( iterations ) ;
221+ const setIndices = new Uint32Array ( iterations ) ;
222+ const hasIndices = new Uint32Array ( iterations ) ;
223+ const deleteIndices = new Uint32Array ( iterations ) ;
224+
225+ for ( let i = 0 ; i < iterations ; i ++ ) {
226+ const idx = i % cacheSize ;
227+ getIndices [ i ] = idx ;
228+ setIndices [ i ] = idx ;
229+ hasIndices [ i ] = idx ;
230+ deleteIndices [ i ] = idx ;
242231 }
243232
244233 let mixedGetIndex = 0 ;
245234 await timer . timeFunction ( "lru.get (mixed workload)" , ( ) => {
246- const i = mixedGetIndex % choice . length ;
247- const pick = choice [ i ] ;
248- if ( pick < 60 ) {
249- const idx = indices [ i ] ;
250- phase2Cache . get ( testData [ idx ] . key ) ;
251- }
235+ const idx = getIndices [ mixedGetIndex % iterations ] ;
236+ phase2Cache . get ( testData [ idx ] . key ) ;
252237 mixedGetIndex ++ ;
253238 } , iterations ) ;
254239
255240 let mixedSetIndex = 0 ;
256241 await timer . timeFunction ( "lru.set (mixed workload)" , ( ) => {
257- const i = mixedSetIndex % choice . length ;
258- const pick = choice [ i ] ;
259- if ( pick >= 60 && pick < 80 ) {
260- const idx = indices [ i ] ;
261- phase2Cache . set ( testData [ idx ] . key , testData [ idx ] . value ) ;
262- }
242+ const idx = setIndices [ mixedSetIndex % iterations ] ;
243+ phase2Cache . set ( testData [ idx ] . key , testData [ idx ] . value ) ;
263244 mixedSetIndex ++ ;
264245 } , iterations ) ;
265246
266247 let mixedHasIndex = 0 ;
267248 await timer . timeFunction ( "lru.has (mixed workload)" , ( ) => {
268- const i = mixedHasIndex % choice . length ;
269- const pick = choice [ i ] ;
270- if ( pick >= 80 && pick < 95 ) {
271- const idx = indices [ i ] ;
272- phase2Cache . has ( testData [ idx ] . key ) ;
273- }
249+ const idx = hasIndices [ mixedHasIndex % iterations ] ;
250+ phase2Cache . has ( testData [ idx ] . key ) ;
274251 mixedHasIndex ++ ;
275252 } , iterations ) ;
276253
277254 let mixedDeleteIndex = 0 ;
278255 await timer . timeFunction ( "lru.delete (mixed workload)" , ( ) => {
279- const i = mixedDeleteIndex % choice . length ;
280- const pick = choice [ i ] ;
281- if ( pick >= 95 ) {
282- const idx = indices [ i ] ;
283- phase2Cache . delete ( testData [ idx ] . key ) ;
284- }
256+ const idx = deleteIndices [ mixedDeleteIndex % iterations ] ;
257+ phase2Cache . delete ( testData [ idx ] . key ) ;
285258 mixedDeleteIndex ++ ;
286259 } , iterations ) ;
287260
0 commit comments