Chris T Chris T - 1 year ago 170
Node.js Question

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

  • Is vm.runInNewContext considered black magic like

  • Is there a
    significant performance difference between
    and reading a
    file and using
    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)

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download