shelljs的性能很慢

我一直在使用shelljs

在我的超高速系统上执行这个:

var shell = require('shelljs') const exec = require('child_process').exec console.time('shell mktemp -d') shell.exec('mktemp -d', {silent: true}) console.timeEnd('shell mktemp -d') console.time('child exec mktemp -d') exec('mktemp', ['-d'], function(error, stdout, stderr) { if (error) { console.error('stderr', stderr) throw error } console.log('exec stdout', stdout) console.timeEnd('child exec mktemp -d') }) 

它给出了以下执行时间:

shell mktemp -d:208.126ms

exec stdout /tmp/tmp.w22tyS5Uyu

孩子执行mktemp -d:48.812ms

为什么shelljs要慢4倍? 有什么想法吗?

看看shelljs是如何实现的: 在这里输入图像说明

它完全依赖于node.js fs库。 这个库是跨平台的,用C ++编写,但不像C语言那么高性能。 更一般地说,你不能在JS中获得C中的perfs …

另一件事,抽象层:你正在使用exec(Command),其中Command是一个C(我认为是Linux C)。 机器创build一个线程并在其中执行一个命令。 当使用shell.js时,有许多机制可以确保交叉平台的forms,并保持命令的抽象为一个函数,并将结果保留为一个variables。 在shell.js中查看exec的代码: https : //github.com/shelljs/shelljs/blob/master/src/exec.js它并不是真的和你的代码行一样。

希望有所帮助!