1
1
import Benchmark from 'benchmark' ;
2
+ import CliTable from 'cli-table' ;
2
3
3
4
import { run as runAutolinkerCurrent } from './autolinker-current/index' ;
4
5
import { 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';
10
11
import { run as runAutolinker2_2_2 } from './autolinker-2.2.2/index' ;
11
12
import { run as runAutolinker1_8_3 } from './autolinker-1.8.3/index' ;
12
13
import { 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' ;
14
16
15
17
const suite = new Benchmark . Suite ( ) ;
16
18
@@ -26,12 +28,47 @@ suite
26
28
. add ( '[email protected] ' , runAutolinker2_2_2 )
27
29
. add ( '[email protected] ' , runAutolinker1_8_3 )
28
30
. add ( '[email protected] ' , runLinkifyIt5_0_0 )
29
- . add ( '[email protected] ' , runLinkifyJs4_2_0 )
31
+ . add ( '[email protected] ' , runLinkifyHtml4_2_0 )
32
+ . add ( '[email protected] ' , runLinkifyString4_2_0 )
30
33
31
34
. on ( 'cycle' , ( event : Benchmark . Event ) => {
32
35
console . log ( String ( event . target ) ) ;
33
36
} )
34
37
. 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
+
35
66
console . log ( 'Fastest is: ' + suite . filter ( 'fastest' ) . map ( 'name' ) ) ;
36
67
} )
37
68
. 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