11'use strict' ; 
22
33const  common  =  require ( '../common' ) ; 
4- const  fs  =  require ( 'fs' ) ; 
4+ const  { 
5+   glob, 
6+   globSync, 
7+   promises : {  glob : globAsync  } , 
8+ }  =  require ( 'fs' ) ; 
59const  path  =  require ( 'path' ) ; 
610const  assert  =  require ( 'node:assert' ) ; 
711
@@ -11,7 +15,7 @@ const configs = {
1115  n : [ 1e3 ] , 
1216  dir : [ 'lib' ] , 
1317  pattern : [ '**/*' ,  '*.js' ,  '**/**.js' ] , 
14-   mode : [ 'async ' ,  'sync ' ] , 
18+   mode : [ 'sync ' ,  'promise'  ,   'callback '] , 
1519  recursive : [ 'true' ,  'false' ] , 
1620} ; 
1721
@@ -20,15 +24,33 @@ const bench = common.createBenchmark(main, configs);
2024async  function  main ( config )  { 
2125  const  fullPath  =  path . resolve ( benchmarkDirectory ,  config . dir ) ; 
2226  const  {  pattern,  recursive,  mode }  =  config ; 
27+   const  options  =  {  cwd : fullPath ,  recursive } ; 
28+   const  callback  =  ( resolve ,  reject )  =>  { 
29+     glob ( pattern ,  options ,  ( err ,  matches )  =>  { 
30+       if  ( err )  { 
31+         reject ( err ) ; 
32+       }  else  { 
33+         resolve ( matches ) ; 
34+       } 
35+     } ) ; 
36+   } ; 
2337
2438  let  noDead ; 
2539  bench . start ( ) ; 
2640
2741  for  ( let  i  =  0 ;  i  <  config . n ;  i ++ )  { 
28-     if  ( mode  ===  'async' )  { 
29-       noDead  =  await  fs . promises . glob ( pattern ,  {  cwd : fullPath ,  recursive } ) ; 
30-     }  else  { 
31-       noDead  =  fs . globSync ( pattern ,  {  cwd : fullPath ,  recursive } ) ; 
42+     switch  ( mode )  { 
43+       case  'sync' :
44+         noDead  =  globSync ( pattern ,  options ) ; 
45+         break ; 
46+       case  'promise' :
47+         noDead  =  await  globAsync ( pattern ,  options ) ; 
48+         break ; 
49+       case  'callback' :
50+         noDead  =  await  new  Promise ( callback ) ; 
51+         break ; 
52+       default :
53+         throw  new  Error ( `Unknown mode: ${ mode }  ` ) ; 
3254    } 
3355  } 
3456
0 commit comments