2017-09-01 9 views
1

結果をフェッチするために2組のコードとgonaが使用されていますが、両方のことができるかどうかを知りたいだけです。モジュールのパフォーマンスに与える影響ノードのクラス

//dataService.js 

module.exports = { 
getData: function(){// return data from some sync source} 
} 

const DataService = require(‘./dataService’); 
console.log(DataService.getData()); 

別の方法

//dataService.js 
var DataService = Class DataService { 
    getData(){/return data from some sync source} 
} 
module.exports = DataService 

const DataService = require(‘./dataService’); 
console.log((new DataService()).getData()); 

我々は100万人以上の要求に負荷をかけたときに、両方のコードは、パフォーマンスとスタンダーの面で結構です、私は理解して助けてください。

+0

あなたは本当にクラスとしてクラスを使用していないので、私は(あなたが要求ごとにそれをインスタンス化し、特に)1を使用してのポイントが表示されていません。 – robertklep

答えて

0

私は、ベンチマークは以下の結果を得た:

Class Data x 22,047,798 ops/sec ±0.88% (88 runs sampled) 
Data as module x 31,695,587 ops/sec ±0.97% (89 runs sampled) 

最速の結果はかなり論理的である

モジュールとして、あなたがたびにクラスをインスタンス化する必要があるデータでありますnewのモジュールでもメモリ消費量が多くなり、新しいクラスがプロトタイプチェインを使用してインスタンスを返すようになります。

ベンチマークコード:

const Benchmark = require('benchmark'); 

const moduleData = require('./modules/module-data'); 
const ClassData = require('./modules/class-data'); 

var suite = new Benchmark.Suite; 

// add tests 
suite.add('Class Data', function() { 
    new ClassData().getData(); 
}) 
    .add('Data as module', function() { 
    moduleData.getData(); 
    }) 
    // add listeners 
    .on('cycle', function (event) { 
    console.log(String(event.target)); 
    }) 
    .on('complete', function() { 
    console.log('Fastest is ' + this.filter('fastest').map('name')); 
    }) 
    // run async 
    .run({ 'async': true }); 
関連する問題