茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    node.js 動態(tài)執(zhí)行腳本
    來源:易賢網(wǎng) 閱讀:1034 次 日期:2016-06-16 17:09:35
    溫馨提示:易賢網(wǎng)小編為您整理了“node.js 動態(tài)執(zhí)行腳本”,方便廣大網(wǎng)友查閱!

    node.js最近新增了虛擬機(jī)模塊,其實也不能說是新增的,只是把一些內(nèi)部接口暴露出來罷了,從2.x就有了。我們可以從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;

     };

    其中的Script對象,就與require('vm')返回的對象很相似,而實質(zhì)上,vm模塊就是對Script對象的封裝。

    代碼如下:

    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 相當(dāng)于一個全新的環(huán)境中執(zhí)行代碼,不會影響當(dāng)前作用域的對象。而runInNewContext與runInContext則能指定是上下文對象,區(qū)別是一個普通對象或一個context對象。換言之,runInNewContext與runInContext能局部影響當(dāng)前作用域的對象。要與當(dāng)前環(huán)境完全進(jìn)行交互的話,就需要用到危險的eval。在node.js自帶的加載體系中,顯然沒有這樣的勇氣,使用的是runInThisContext。并且在這之前做了許多工作,如把用戶的JS文件里面的內(nèi)容再包一層( NativeModule.wrap),還有其他凌散操作,加之是同步操作,實際上是一種效率很糟的加載方式。唯一的好處是,使用了同步,讓代碼編寫起來簡單多了。

    在github中,已有人對這幾種動態(tài)執(zhí)行腳本的方法進(jìn)行性能比較:

    代碼如下:

    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: 212ms

    vm.runInNewContext: 2222ms

    script.runInThisContext: 6ms

    script.runInNewContext: 1876ms

    script.runInContext: 44ms

    fn: 0ms

    */

    由此可見,還是v8自帶的方法Function完勝!

    以上就是本文的全部內(nèi)容,希望能給大家一個參考

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:node.js 動態(tài)執(zhí)行腳本
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報班

    • 報班類型
    • 姓名
    • 手機(jī)號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)