import * as fs from 'fs';
import * as path from 'path';
function list_dir(dir: string) {
fs.readdir(dir, 'utf-8', (err, files) => {
files.forEach(file => {
file = path.join(dir, file)
fs.stat(file, (err, stat) => {
if (stat.isDirectory()) {
list_dir(file)
}
if (stat.isFile()) {
file
// console.log("%O", file);
}
})
});
})
}
list_dir('.')
import * as util from 'util';
import * as fs from 'fs';
import * as path from 'path';
async function list_dir(dir: string) {
const readdir = util.promisify(fs.readdir);
const stat = util.promisify(fs.stat);
const files = await readdir(dir, 'utf-8')
files.forEach(async file => {
file = path.join(dir, file)
const state = await stat(file)
if (state.isDirectory()) {
await list_dir(file)
}
if (state.isFile()) {
file
// console.log("%O", file);
}
})
}
list_dir('.')
node ➜ /workspaces/typescript $ time node promise.js && time node callback.js
real 0m1.140s
user 0m1.136s
sys 0m1.292s
real 0m0.377s
user 0m0.368s
sys 0m0.534s
node ➜ /workspaces/typescript $ time node promise.js && time node callback.js
real 0m1.062s
user 0m1.132s
sys 0m1.184s
real 0m0.538s
user 0m0.470s
sys 0m0.784s
node ➜ /workspaces/typescript $ time node promise.js && time node callback.js
real 0m1.194s
user 0m1.221s
sys 0m1.308s
real 0m0.436s
user 0m0.393s
sys 0m0.651s
node ➜ /workspaces/typescript $ time node promise.js && time node callback.js
real 0m1.024s
user 0m1.165s
sys 0m1.027s
real 0m0.416s
user 0m0.313s
sys 0m0.653s
node ➜ /workspaces/typescript $ nodejs --version
v16.3.0
node ➜ /workspaces/typescript $ tsc --version
Version 4.3.5
有性能差异可以理解,但是这个性能差异过大,请问各位是我的写法有问题,还是其它原因导致的呢?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.