@@ -95,18 +95,29 @@ describe('Problem 2618: Check If Instance Of Class', () => {
9595 const Dog = class extends Object { } ;
9696 const input : [ any , any ] = [ new Dog ( ) , Object ] ;
9797
98- const benchmark = ( label : string , fn : InstanceOfFunction ) => {
98+ const benchmark = ( label : string , fn : InstanceOfFunction ) : number => {
9999 const start = performance . now ( ) ;
100100 for ( let i = 0 ; i < iterations ; i ++ ) {
101101 fn ( ...input ) ;
102102 }
103103 const end = performance . now ( ) ;
104- console . log ( ` ${ label } took ${ ( end - start ) . toFixed ( 2 ) } ms for ${ iterations } iterations` ) ;
104+ return end - start ;
105105 } ;
106106
107- test ( 'Compare performance' , ( ) => {
108- benchmark ( 'checkIfInstanceOf' , checkIfInstanceOf ) ;
109- benchmark ( 'checkIfInstanceOfUsingPrototype' , checkIfInstanceOfUsingPrototype ) ;
107+ // Run benchmarks first, store times
108+ const timeA = benchmark ( 'checkIfInstanceOf' , checkIfInstanceOf ) ;
109+ const timeB = benchmark ( 'checkIfInstanceOfUsingPrototype' , checkIfInstanceOfUsingPrototype ) ;
110+
111+ const slower = timeA > timeB ? 'checkIfInstanceOf' : 'checkIfInstanceOfUsingPrototype' ;
112+ const factor = ( Math . max ( timeA , timeB ) / Math . min ( timeA , timeB ) ) . toFixed ( 1 ) ;
113+
114+ // Now the test name includes the summary info
115+ test ( `Performance: checkIfInstanceOf took ${ timeA . toFixed ( 2 ) } ms and checkIfInstanceOfUsingPrototype took ${ timeB . toFixed ( 2 ) } ms` , ( ) => {
116+ expect ( true ) . toBe ( true ) ;
117+ } ) ;
118+
119+ test ( `Comparison: ${ slower } is ${ factor } x slower` , ( ) => {
120+ expect ( true ) . toBe ( true ) ;
110121 } ) ;
111122 } ) ;
112123} ) ;
0 commit comments