11'use strict' ;
22
33const common = require ( '../common.js' ) ;
4- const ClientRequest = require ( 'http' ) . ClientRequest ;
4+ const { ClientRequest } = require ( 'http' ) ;
5+ const assert = require ( 'assert' ) ;
6+
57const types = Object . keys ( common . urls )
68 . filter ( ( i ) => common . urls [ i ]
79 . startsWith ( 'http://' ) ) ;
@@ -15,36 +17,43 @@ const bench = common.createBenchmark(main, {
1517function noop ( ) { }
1618
1719function main ( { url : type , arg, e } ) {
18- e = + e ;
20+ e = Number ( e ) ;
1921 const data = common . bakeUrlData ( type , e , false , false )
2022 . filter ( ( i ) => i . startsWith ( 'http://' ) ) ;
2123 const len = data . length ;
22- var result ;
23- var i ;
24- if ( arg === 'options' ) {
25- const options = data . map ( ( i ) => ( {
26- path : new URL ( i ) . path , createConnection : noop
27- } ) ) ;
28- bench . start ( ) ;
29- for ( i = 0 ; i < len ; i ++ ) {
30- result = new ClientRequest ( options [ i ] ) ;
24+ let result ;
25+ switch ( arg ) {
26+ case 'options' : {
27+ const options = data . map ( ( i ) => ( {
28+ path : new URL ( i ) . path , createConnection : noop
29+ } ) ) ;
30+ bench . start ( ) ;
31+ for ( let i = 0 ; i < len ; i ++ ) {
32+ result = new ClientRequest ( options [ i ] ) ;
33+ }
34+ bench . end ( len ) ;
35+ break ;
36+ }
37+ case 'URL' : {
38+ const options = data . map ( ( i ) => new URL ( i ) ) ;
39+ bench . start ( ) ;
40+ for ( let i = 0 ; i < len ; i ++ ) {
41+ result = new ClientRequest ( options [ i ] , { createConnection : noop } ) ;
42+ }
43+ bench . end ( len ) ;
44+ break ;
3145 }
32- bench . end ( len ) ;
33- } else if ( arg === 'URL' ) {
34- const options = data . map ( ( i ) => new URL ( i ) ) ;
35- bench . start ( ) ;
36- for ( i = 0 ; i < len ; i ++ ) {
37- result = new ClientRequest ( options [ i ] , { createConnection : noop } ) ;
46+ case 'string' : {
47+ bench . start ( ) ;
48+ for ( let i = 0 ; i < len ; i ++ ) {
49+ result = new ClientRequest ( data [ i ] , { createConnection : noop } ) ;
50+ }
51+ bench . end ( len ) ;
52+ break ;
3853 }
39- bench . end ( len ) ;
40- } else if ( arg === 'string' ) {
41- bench . start ( ) ;
42- for ( i = 0 ; i < len ; i ++ ) {
43- result = new ClientRequest ( data [ i ] , { createConnection : noop } ) ;
54+ default : {
55+ throw new Error ( `Unknown arg type ${ arg } ` ) ;
4456 }
45- bench . end ( len ) ;
46- } else {
47- throw new Error ( `Unknown arg type ${ arg } ` ) ;
4857 }
49- require ( ' assert' ) . ok ( result ) ;
58+ assert . ok ( result ) ;
5059}
0 commit comments