Node.js recently added a virtual machine module, in fact, can not be said to be new, just to expose some of the internal interface, from the 2.x have. We can see this 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 in it is similar to the object returned by the Require (' VM '), in essence, the VM module is the encapsulation of 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]}
Where Runinthiscontext is equivalent to executing code in a completely new environment without affecting the objects of the current scope. While Runinnewcontext and Runincontext can specify a context object, the difference is a normal object or a contextual object. In other words, runinnewcontext and runincontext can partially affect objects in the current scope. To fully interact with the current environment, you need to use a dangerous eval. In the Node.js loading system, there is obviously no such courage, the use of runinthiscontext. And before this done a lot of work, such as the user's JS file inside the contents of a layer (NATIVEMODULE.WRAP), there are other operations, coupled with the synchronous operation, is actually a very inefficient loading mode. The only advantage is that using synchronization makes the code much simpler to write.
In GitHub, there has been a performance comparison of these dynamic script execution methods:
var vm = require (' VM '), code = ' var square = n * n; ', fn = new Function (' n ', code), script = Vm.createscript (code), s
Andbox;
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:212ms vm.runinnewcontext:2222ms script.runinthiscontext:6ms script.runinnewcontext:1876ms Script.runincontext:44ms fn:0ms
*/
This shows, or V8 's own method of the function of the victory!
The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.