Skip to content

Commit 78010bc

Browse files
committed
feat: modify test and bench file
1 parent fb0fdeb commit 78010bc

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

benchmark/readdir.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@ bench
2222
.add('hyper-fs readdirSync (default)', () => {
2323
readdirSync(dir)
2424
})
25-
.add('hyper-fs readdirSync (2 threads)', () => {
26-
readdirSync(dir, { concurrency: 2 })
25+
.add('hyper-fs readdirSync (withFileTypes)', () => {
26+
readdirSync(dir, { withFileTypes: true })
2727
})
28-
.add('hyper-fs readdirSync (2 threads, recursive)', () => {
29-
readdirSync(dir, { concurrency: 2, recursive: true })
28+
.add('hyper-fs readdirSync (recursive)', () => {
29+
readdirSync(dir, { recursive: true })
3030
})
31-
.add('hyper-fs readdirSync (4 threads)', () => {
32-
readdirSync(dir, { concurrency: 4 })
31+
.add('hyper-fs readdirSync (recursive, withFileTypes)', () => {
32+
readdirSync(dir, { recursive: true, withFileTypes: true })
3333
})
3434
.add('hyper-fs readdirSync (4 threads, recursive)', () => {
3535
readdirSync(dir, { concurrency: 4, recursive: true })
3636
})
37+
.add('hyper-fs readdirSync (4 threads, recursive, withFileTypes)', () => {
38+
readdirSync(dir, { concurrency: 4, recursive: true, withFileTypes: true })
39+
})
3740
await bench.run()
3841

3942
console.table(bench.table())

src/readdir.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,40 @@ fn ls(
6060
let parent_path_val = search_path_str.to_string();
6161
let entries = fs::read_dir(path).map_err(|e| Error::from_reason(e.to_string()))?;
6262

63-
if with_file_types {
64-
let mut result = Vec::with_capacity(64);
65-
for entry in entries {
66-
let entry = entry.map_err(|e| Error::from_reason(e.to_string()))?;
67-
let file_name = entry.file_name();
68-
let name_str = file_name.to_string_lossy();
69-
if skip_hidden && name_str.starts_with('.') {
70-
continue;
71-
}
72-
result.push(Dirent {
63+
let mut result_files = if with_file_types {
64+
None
65+
} else {
66+
Some(Vec::with_capacity(64))
67+
};
68+
let mut result_dirents = if with_file_types {
69+
Some(Vec::with_capacity(64))
70+
} else {
71+
None
72+
};
73+
74+
for entry in entries {
75+
let entry = entry.map_err(|e| Error::from_reason(e.to_string()))?;
76+
let file_name = entry.file_name();
77+
let name_str = file_name.to_string_lossy();
78+
if skip_hidden && name_str.starts_with('.') {
79+
continue;
80+
}
81+
82+
if let Some(ref mut list) = result_dirents {
83+
list.push(Dirent {
7384
name: name_str.to_string(),
7485
parent_path: parent_path_val.clone(),
7586
is_dir: entry.file_type().map(|t| t.is_dir()).unwrap_or(false),
7687
});
88+
} else if let Some(ref mut list) = result_files {
89+
list.push(name_str.to_string());
7790
}
78-
return Ok(Either::B(result));
91+
}
92+
93+
if with_file_types {
94+
return Ok(Either::B(result_dirents.unwrap()));
7995
} else {
80-
let mut result = Vec::with_capacity(64);
81-
for entry in entries {
82-
let entry = entry.map_err(|e| Error::from_reason(e.to_string()))?;
83-
let file_name = entry.file_name();
84-
let name_str = file_name.to_string_lossy();
85-
if skip_hidden && name_str.starts_with('.') {
86-
continue;
87-
}
88-
result.push(name_str.to_string());
89-
}
90-
return Ok(Either::A(result));
96+
return Ok(Either::A(result_files.unwrap()));
9197
}
9298
}
9399

@@ -123,8 +129,7 @@ fn ls(
123129
Ok(Either::B(result))
124130
} else {
125131
// When recursive is true and withFileTypes is false, Node.js returns relative paths.
126-
// jwalk entries have full paths.
127-
// We need to strip the root path.
132+
// But jwalk entries have full paths, We need to strip the root path.
128133
let root = path;
129134
let result = walk_dir
130135
.into_iter()

0 commit comments

Comments
 (0)