search

Home  >  Q&A  >  body text

node.js - nodejs performance is poor

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梦2756 days ago1337

reply all(3)I'll reply

  • 代言

    代言2017-06-20 10:08:41

    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.

    reply
    0
  • 欧阳克

    欧阳克2017-06-20 10:08:41

    You can use node’s built-in module fs to traverse.

    reply
    0
  • PHP中文网

    PHP中文网2017-06-20 10:08:41

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

    reply
    0
  • Cancelreply