Node.JS vm.runInNewContext() vs require() and eval()

前端 未结 2 548
再見小時候
再見小時候 2021-02-15 17:02
  • Is vm.runInNewContext considered black magic like eval?
  • Is there a significant performance difference between require and reading a file
2条回答
  •  清歌不尽
    2021-02-15 17:22

    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.

    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.

提交回复
热议问题