11import Benchmark from 'benchmark' ;
2+ import CliTable from 'cli-table' ;
23
34import { run as runAutolinkerCurrent } from './autolinker-current/index' ;
45import { run as runAutolinker4_1_1 } from './autolinker-4.1.0/index' ;
@@ -10,7 +11,8 @@ import { run as runAutolinker3_16_2 } from './autolinker-3.16.2/index';
1011import { run as runAutolinker2_2_2 } from './autolinker-2.2.2/index' ;
1112import { run as runAutolinker1_8_3 } from './autolinker-1.8.3/index' ;
1213import { run as runLinkifyIt5_0_0 } from './linkify-it-5.0.0/index' ;
13- import { run as runLinkifyJs4_2_0 } from './linkify-js-4.2.0/index' ;
14+ import { run as runLinkifyHtml4_2_0 } from './linkify-html-4.2.0/index' ;
15+ import { run as runLinkifyString4_2_0 } from './linkify-string-4.2.0/index' ;
1416
1517const suite = new Benchmark . Suite ( ) ;
1618
@@ -26,12 +28,47 @@ suite
2628 . add ( 'autolinker@2.2.2' , runAutolinker2_2_2 )
2729 . add ( 'autolinker@1.8.3' , runAutolinker1_8_3 )
2830 . add ( 'linkify-it@5.0.0' , runLinkifyIt5_0_0 )
29- . add ( 'linkify-js@4.2.0' , runLinkifyJs4_2_0 )
31+ . add ( 'linkify-html@4.2.0' , runLinkifyHtml4_2_0 )
32+ . add ( 'linkify-string@4.2.0' , runLinkifyString4_2_0 )
3033
3134 . on ( 'cycle' , ( event : Benchmark . Event ) => {
3235 console . log ( String ( event . target ) ) ;
3336 } )
3437 . on ( 'complete' , ( ) => {
38+ console . log ( '-------------------------------------' ) ;
39+
40+ const results : Result [ ] = suite . map ( ( bench : Benchmark ) => {
41+ return {
42+ name : bench . name ,
43+ ops : bench . hz ,
44+ margin : '±' + bench . stats . rme . toFixed ( 2 ) + '%' ,
45+ } ;
46+ } ) ;
47+ results . sort ( ( a , b ) => b . ops - a . ops ) ;
48+
49+ const fastest = results [ 0 ] ;
50+
51+ const table = new CliTable ( {
52+ head : [ 'Library' , 'Ops/sec' , 'MOE' , 'Compared to Fastest' ] ,
53+ } ) ;
54+ results . forEach ( result => {
55+ table . push ( [
56+ result . name ,
57+ Benchmark . formatNumber ( Math . floor ( result . ops ) ) ,
58+ result . margin ,
59+ result === fastest
60+ ? 'Fastest ✅'
61+ : Math . floor ( ( 1 - result . ops / fastest . ops ) * 100 ) + '% slower' ,
62+ ] ) ;
63+ } ) ;
64+ console . log ( table . toString ( ) ) ;
65+
3566 console . log ( 'Fastest is: ' + suite . filter ( 'fastest' ) . map ( 'name' ) ) ;
3667 } )
3768 . run ( { async : true , minSamples : 100 } ) ;
69+
70+ interface Result {
71+ name : string ;
72+ ops : number ; // operations per second
73+ margin : string ;
74+ }
0 commit comments