To avoid this, wrap scripts that you run using runInContext or runInNewContext in a immediately-invoked function expression (IIFE).
we should wrap code with function(){...} for some reason to improve the performance.
Also, the implementation of require in Node.js wraps code with function(){...} and run it with vm.runInThisContext internally (reference). Thus, it seems like we also need to wrap code with function(){...} though I don't understand the exact reason.
The execution of code below is slower than Python.
According to Performance section of this blog:
we should wrap code with
function(){...}
for some reason to improve the performance.Also, the implementation of
require
in Node.js wraps code withfunction(){...}
and run it withvm.runInThisContext
internally (reference). Thus, it seems like we also need to wrap code withfunction(){...}
though I don't understand the exact reason.