Node. js dynamically executes the script, and node. js executes the script
Node. js has recently added Virtual Machine modules. In fact, it cannot be said to be new. It just exposes some internal interfaces, from 2. x. We can see the code from node/src/node. js:
var Script = process.binding('evals').NodeScript;var runInThisContext = Script.runInThisContext; NativeModule.wrap = function(script) { return NativeModule.wrapper[0] + script + NativeModule.wrapper[1]; }; NativeModule.wrapper = [ '(function (exports, require, module, __filename, __dirname) { ', '\n});' ]; NativeModule.prototype.compile = function() { var source = NativeModule.getSource(this.id); source = NativeModule.wrap(source); var fn = runInThisContext(source, this.filename, true); fn(this.exports, NativeModule.require, this, this.filename); this.loaded = true; };
The Script object is similar to the object returned by require ('vm '). In essence, the vm module encapsulates the Script object.
var Script = process.binding('evals').NodeScript;console.log(Script)/**{ [Function: NodeScript] createContext: [Function], runInContext: [Function], runInThisContext: [Function], runInNewContext: [Function] } */console.log(require('vm')){ Script: { [Function: NodeScript] createContext: [Function], runInContext: [Function], runInThisContext: [Function], runInNewContext: [Function] }, createScript: [Function], createContext: [Function], runInContext: [Function], runInThisContext: [Function], runInNewContext: [Function] }
RunInThisContext is equivalent to executing code in a completely new environment and does not affect the objects in the current scope. RunInNewContext and runInContext can be specified as context objects. The difference is a common object or a context object. In other words, runInNewContext and runInContext can partially affect objects in the current scope. To fully interact with the current environment, dangerous eval is required. In the built-in Loading System of node. js, there is obviously no such courage. runInThisContext is used. And a lot of work has been done before, such as adding the content in the user's JS file to another layer (NativeModule. wrap), there are other scattered operations, coupled with synchronization operations, is actually a very bad loading method. The only benefit is that the use of synchronization makes code much easier.
On github, performance comparison of these dynamic script execution methods has been performed:
var vm = require('vm'), code = 'var square = n * n;', fn = new Function('n', code), script = vm.createScript(code), sandbox; n = 5;sandbox = { n: n }; benchmark = function(title, funk) { var end, i, start; start = new Date; for (i = 0; i < 5000; i++) { funk(); } end = new Date; console.log(title + ': ' + (end - start) + 'ms');} var ctx = vm.createContext(sandbox);benchmark('vm.runInThisContext', function() { vm.runInThisContext(code); });benchmark('vm.runInNewContext', function() { vm.runInNewContext(code, sandbox); });benchmark('script.runInThisContext', function() { script.runInThisContext(); });benchmark('script.runInNewContext', function() { script.runInNewContext(sandbox); });benchmark('script.runInContext', function() { script.runInContext(ctx); });benchmark('fn', function() { fn(n); });/**vm.runInThisContext: 212msvm.runInNewContext: 2222msscript.runInThisContext: 6msscript.runInNewContext: 1876msscript.runInContext: 44msfn: 0ms */
It can be seen that the built-in Function of v8 is perfect!
The above is all the content of this article. I hope you can give us a reference and support the customer's home.