@@ -92,10 +92,11 @@ async function runPerformanceObserverBenchmarks () {
9292 // Phase 1: Fill cache with initial data
9393 console . log ( "Phase 1: Initial cache population" ) ;
9494 const phase1Cache = lru ( cacheSize ) ;
95+ let phase1Index = 0 ;
9596 await timer . timeFunction ( "lru.set (initial population)" , ( ) => {
96- for ( let i = 0 ; i < cacheSize ; i ++ ) {
97- phase1Cache . set ( testData [ i ] . key , testData [ i ] . value ) ;
98- }
97+ const i = phase1Index % cacheSize ;
98+ phase1Cache . set ( testData [ i ] . key , testData [ i ] . value ) ;
99+ phase1Index ++ ;
99100 } , 10000 ) ;
100101
101102 // Phase 2: Mixed read/write operations
@@ -118,53 +119,58 @@ async function runPerformanceObserverBenchmarks () {
118119 indices [ i ] = r % testData . length ;
119120 }
120121
122+ let mixedGetIndex = 0 ;
121123 await timer . timeFunction ( "lru.get (mixed workload)" , ( ) => {
122- for ( let i = 0 ; i < 5000 ; i ++ ) {
123- const pick = choice [ i ] ;
124- if ( pick < 60 ) {
125- const idx = indices [ i ] ;
126- phase2Cache . get ( testData [ idx ] . key ) ;
127- }
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 ) ;
128129 }
130+ mixedGetIndex ++ ;
129131 } , 10000 ) ;
130132
133+ let mixedSetIndex = 0 ;
131134 await timer . timeFunction ( "lru.set (mixed workload)" , ( ) => {
132- for ( let i = 0 ; i < 5000 ; i ++ ) {
133- const pick = choice [ i ] ;
134- if ( pick >= 60 && pick < 80 ) {
135- const idx = indices [ i ] ;
136- phase2Cache . set ( testData [ idx ] . key , testData [ idx ] . value ) ;
137- }
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 ) ;
138140 }
141+ mixedSetIndex ++ ;
139142 } , 10000 ) ;
140143
144+ let mixedHasIndex = 0 ;
141145 await timer . timeFunction ( "lru.has (mixed workload)" , ( ) => {
142- for ( let i = 0 ; i < 5000 ; i ++ ) {
143- const pick = choice [ i ] ;
144- if ( pick >= 80 && pick < 95 ) {
145- const idx = indices [ i ] ;
146- phase2Cache . has ( testData [ idx ] . key ) ;
147- }
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 ) ;
148151 }
152+ mixedHasIndex ++ ;
149153 } , 10000 ) ;
150154
155+ let mixedDeleteIndex = 0 ;
151156 await timer . timeFunction ( "lru.delete (mixed workload)" , ( ) => {
152- for ( let i = 0 ; i < 5000 ; i ++ ) {
153- const pick = choice [ i ] ;
154- if ( pick >= 95 ) {
155- const idx = indices [ i ] ;
156- phase2Cache . delete ( testData [ idx ] . key ) ;
157- }
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 ) ;
158162 }
163+ mixedDeleteIndex ++ ;
159164 } , 10000 ) ;
160165
161166 // Phase 3: Cache eviction stress test
162167 console . log ( "Phase 3: Cache eviction stress test" ) ;
163168 const phase3Cache = lru ( cacheSize ) ;
169+ let phase3Index = 0 ;
164170 await timer . timeFunction ( "lru.set (eviction stress)" , ( ) => {
165- for ( let i = 0 ; i < cacheSize ; i ++ ) {
166- phase3Cache . set ( `evict_key_${ i } ` , `evict_value_${ i } ` ) ;
167- }
171+ const i = phase3Index ;
172+ phase3Cache . set ( `evict_key_${ i } ` , `evict_value_${ i } ` ) ;
173+ phase3Index ++ ;
168174 } , 10000 ) ;
169175
170176 // Phase 4: Some clear operations
0 commit comments