11import { performance } from "node:perf_hooks" ;
22import { lru } from "../dist/tiny-lru.js" ;
33
4+ // Custom high-resolution timer benchmark (alternative approach)
5+ class CustomTimer {
6+ constructor ( ) {
7+ this . results = new Map ( ) ;
8+ }
9+
10+ async timeFunction ( name , fn , iterations = 1000 ) {
11+ const times = [ ] ;
12+
13+ // Warmup
14+ for ( let i = 0 ; i < Math . min ( 100 , iterations / 10 ) ; i ++ ) {
15+ await fn ( ) ;
16+ }
17+
18+ // Actual measurement
19+ for ( let i = 0 ; i < iterations ; i ++ ) {
20+ const start = performance . now ( ) ;
21+ await fn ( ) ;
22+ const end = performance . now ( ) ;
23+ times . push ( end - start ) ;
24+ }
25+
26+ // Calculate statistics
27+ const totalTime = times . reduce ( ( a , b ) => a + b , 0 ) ;
28+ const avgTime = totalTime / iterations ;
29+ const minTime = Math . min ( ...times ) ;
30+ const maxTime = Math . max ( ...times ) ;
31+
32+ const sorted = [ ...times ] . sort ( ( a , b ) => a - b ) ;
33+ const median = sorted [ Math . floor ( sorted . length / 2 ) ] ;
34+
35+ const variance = times . reduce ( ( acc , time ) => acc + Math . pow ( time - avgTime , 2 ) , 0 ) / iterations ;
36+ const stdDev = Math . sqrt ( variance ) ;
37+
38+ this . results . set ( name , {
39+ name,
40+ iterations,
41+ avgTime,
42+ minTime,
43+ maxTime,
44+ median,
45+ stdDev,
46+ opsPerSec : 1000 / avgTime // Convert ms to ops/sec
47+ } ) ;
48+ }
49+
50+ printResults ( ) {
51+ console . log ( "\n⏱️ Custom Timer Results" ) ;
52+ console . log ( "========================" ) ;
53+
54+ const results = Array . from ( this . results . values ( ) ) ;
55+ console . table ( results . map ( r => ( {
56+ "Operation" : r . name ,
57+ "Iterations" : r . iterations ,
58+ "Avg (ms)" : r . avgTime . toFixed ( 6 ) ,
59+ "Min (ms)" : r . minTime . toFixed ( 6 ) ,
60+ "Max (ms)" : r . maxTime . toFixed ( 6 ) ,
61+ "Median (ms)" : r . median . toFixed ( 6 ) ,
62+ "Std Dev" : r . stdDev . toFixed ( 6 ) ,
63+ "Ops/sec" : Math . round ( r . opsPerSec )
64+ } ) ) ) ;
65+ }
66+ }
67+
468// Test data generation
569function generateTestData ( size ) {
670 const out = new Array ( size ) ;
@@ -101,7 +165,7 @@ async function runPerformanceObserverBenchmarks () {
101165 for ( let i = 0 ; i < cacheSize ; i ++ ) {
102166 phase3Cache . set ( `evict_key_${ i } ` , `evict_value_${ i } ` ) ;
103167 }
104- } , 1000 ) ;
168+ } , 10000 ) ;
105169
106170 // Phase 4: Some clear operations
107171 console . log ( "Phase 4: Clear operations" ) ;
@@ -111,7 +175,7 @@ async function runPerformanceObserverBenchmarks () {
111175 cache . set ( `temp_${ j } ` , `temp_value_${ j } ` ) ;
112176 }
113177 cache . clear ( ) ;
114- } , 1000 ) ;
178+ } , 10000 ) ;
115179
116180 // Print results with Performance Observer header
117181 console . log ( "\n📊 Performance Observer Results" ) ;
@@ -130,70 +194,6 @@ async function runPerformanceObserverBenchmarks () {
130194 } ) ) ) ;
131195}
132196
133- // Custom high-resolution timer benchmark (alternative approach)
134- class CustomTimer {
135- constructor ( ) {
136- this . results = new Map ( ) ;
137- }
138-
139- async timeFunction ( name , fn , iterations = 1000 ) {
140- const times = [ ] ;
141-
142- // Warmup
143- for ( let i = 0 ; i < Math . min ( 100 , iterations / 10 ) ; i ++ ) {
144- await fn ( ) ;
145- }
146-
147- // Actual measurement
148- for ( let i = 0 ; i < iterations ; i ++ ) {
149- const start = performance . now ( ) ;
150- await fn ( ) ;
151- const end = performance . now ( ) ;
152- times . push ( end - start ) ;
153- }
154-
155- // Calculate statistics
156- const totalTime = times . reduce ( ( a , b ) => a + b , 0 ) ;
157- const avgTime = totalTime / iterations ;
158- const minTime = Math . min ( ...times ) ;
159- const maxTime = Math . max ( ...times ) ;
160-
161- const sorted = [ ...times ] . sort ( ( a , b ) => a - b ) ;
162- const median = sorted [ Math . floor ( sorted . length / 2 ) ] ;
163-
164- const variance = times . reduce ( ( acc , time ) => acc + Math . pow ( time - avgTime , 2 ) , 0 ) / iterations ;
165- const stdDev = Math . sqrt ( variance ) ;
166-
167- this . results . set ( name , {
168- name,
169- iterations,
170- avgTime,
171- minTime,
172- maxTime,
173- median,
174- stdDev,
175- opsPerSec : 1000 / avgTime // Convert ms to ops/sec
176- } ) ;
177- }
178-
179- printResults ( ) {
180- console . log ( "\n⏱️ Custom Timer Results" ) ;
181- console . log ( "========================" ) ;
182-
183- const results = Array . from ( this . results . values ( ) ) ;
184- console . table ( results . map ( r => ( {
185- "Operation" : r . name ,
186- "Iterations" : r . iterations ,
187- "Avg (ms)" : r . avgTime . toFixed ( 6 ) ,
188- "Min (ms)" : r . minTime . toFixed ( 6 ) ,
189- "Max (ms)" : r . maxTime . toFixed ( 6 ) ,
190- "Median (ms)" : r . median . toFixed ( 6 ) ,
191- "Std Dev" : r . stdDev . toFixed ( 6 ) ,
192- "Ops/sec" : Math . round ( r . opsPerSec )
193- } ) ) ) ;
194- }
195- }
196-
197197async function runCustomTimerBenchmarks ( ) {
198198 console . log ( "\n⚡ Custom Timer Benchmarks" ) ;
199199 console . log ( "==========================" ) ;
0 commit comments