node.js - nodejs performance is poor
过去多啦不再A梦
过去多啦不再A梦 2017-06-20 10:06:41
0
3
1261

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! ! ! !

过去多啦不再A梦
过去多啦不再A梦

reply all(3)
代言

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.

Ty80

The biggest advantage of node is asynchronous, but you use a synchronous method

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!