I am a beginner and found that the performance of nodejs is very poor! ! !
var rd=require('rd');
var files = rd.readSync('/home');
// 异步遍历目录下的所有文件
rd.each('/home', function (f, s, next) {
// 每找到一个文件都会调用一次此函数
// 参数s是通过 fs.stat() 获取到的文件属性值
console.log('file: %s', f);
// 必须调用next()才能继续
next();
}, function (err) {
if (err) throw err;
// 完成
});
Use the above code to traverse all the files in the home directory. There are only 140,000 files under /home. Nodejs will get stuck and die in the middle.
Python can print all files to the console in 40 seconds.
Is it still necessary to learn nodejs?
The following code comes from stackoverflow.
var fs = require('fs');
var path = require('path');
var walk = function(directoryName) {
fs.readdir(directoryName, function(e, files) {
files.forEach(function(file) {
fs.stat(directoryName + path.sep + file, function(e, f) {
if (f.isDirectory()) {
walk(directoryName + path.sep + file)
} else {
console.log(' - ' + file)
}
})
})
})
}
walk("/home")
Also stuck, nodejs does not have any performance advantages, it is a disadvantage. It is not terrible to run slower, it will get stuck! ! ! !
I think the poster made a rash comment on a language without thinking deeply.
readSync is a synchronization method. When it encounters a large file and reads it at once, the memory will burst, so it will make you feel stuck.
The correct way should be to use stream to read and write files.
You can use node’s built-in module fs to traverse.
The biggest advantage of node is asynchronous, but you use a synchronous method