Node.JS vm.runInNewContext()与require()和eval() [英] Node.JS vm.runInNewContext() vs require() and eval()

查看:646
本文介绍了Node.JS vm.runInNewContext()与require()和eval()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • vm.runInNewContext是否被视为黑魔法,如 eval

  • 是否有
    要求和读取
    文件并使用 vm 运行它之间的显着性能差异是相同(如果
    您实现了缓存等,只是想向
    上下文中添加一些变量)

  • Is vm.runInNewContext considered black magic like eval?
  • Is there a significant performance difference between require and reading a file and using vm to run it or is the the same under the hood (if you implemented caching etc and just wanted to add some variables to the context)

推荐答案

runInNewContext 并不是要代替 require eval ,但作为一种创建沙箱环境的方式,您可以在其中安全地运行其他脚本。

runInNewContext is not meant to be used as a replacement of require or eval, but instead as a way to create a sandbox environment where you can safely run other scripts.

缺点它的速度很慢(创建大约需要10毫秒),并且占用了几兆字节。因此,不,请勿将其用作要求的替代品。

Disadvantages are that it's slow (creation takes ~10 ms.) and takes up a couple megabytes. So no, don't use it as a require replacement.

这篇关于Node.JS vm.runInNewContext()与require()和eval()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆