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

前端 未结 2 555
再見小時候
再見小時候 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:32

    If you check out the code that implements loading Modules in node.js, you'll see that require uses vm.runInNewContext or vm.runInThisContext under the hood. The require however, does some other extra things, like caching the module.

    The node documentation shows how the behavior is similar and different between the vm commands and eval.

    So, require, eval and vm are all a little bit different, but all can be used to load code. They all have similar security issues if you are loading arbitrary code that comes from the client.

提交回复
热议问题