JavaScriptのvarとconstとついでにhash-objectのベンチマーク+おまけ追記(ループベンチマーク)
例によって唐突に結果ぺたり。
LH-MAIN /tmp% node benchmark_const.js N = 1000000000 9678 [ms] cost of nothing 9379 [ms] cost of 1000000000-th bind var from var 10854 [ms] cost of 1000000000-th bind var from const 11190 [ms] cost of 1000000000-th bind var from hash-oject LH-MAIN /tmp% node --version v0.6.13
いやはや…。ちなみにこのベンチマークの元ネタは2007年の次の記事です。2012年、Node.jsではどうか、という訳でやってみたわけですが(´・ω・`)
何をした結果なのか?
↓のソースをnode.jsで実行した結果。const教徒でも実効速度がvarより遅くなる、つまりconstの方が若干であれ何らかのオーバーヘッドのある実装になっている可能性があるとなればこれはconst使いたくないでござるな・x・;
そもそもループ変数の処理が重い事の方に気が行くけど、それはほら高階関数でループすれば…どうなんでしょうねこの子。まあ、それはまた気が向いたらやってみましょう。
ソース
var main = function(){ const N = 1000 * 1000 * 1000; console.log( 'N = ' + N ); var a = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; const b = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; var c = { v: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' }; var benchmark = function(f, a){ var start = new Date(); var n = N; while(--n) f(); var end = new Date(); console.log( ( end.getTime() - start.getTime() ) + ' [ms] ' + 'cost of ' + a ); }; benchmark( function(){}, 'nothing' ); benchmark( function(){ var x = a; }, N + '-th bind var from var' ); benchmark( function(){ var x = b; }, N + '-th bind var from const' ); benchmark( function(){ var x = c.v; }, N + '-th bind var from hash-oject' ); } main();
おまけ追記(ループベンチマーク)
結果
LH-MAIN /tmp% node benchmark_loop.js N = 16384 0 [ms] cost of 16384-th proc with while loop 64 [ms] cost of 16384-th proc with higher-order function(argument.callee) LH-MAIN /tmp% node --version v0.6.13
/(^o^)\
ソース
// ©2012 Usagi Ito / Wonder Rabbit Project, License is MIT/X11 // Usagi Ito <usagi@WonderRabbitProject.net> // this source delivered from: // https://gist.github.com/2274188 var main = function(){ const N = 16 * 1024; console.log( 'N = ' + N ); var a = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'; var benchmark = function(f, a){ var start = new Date(); f(); var end = new Date(); console.log( ( end.getTime() - start.getTime() ) + ' [ms] ' + 'cost of ' + a ); }; var proc = function(){ var x = a; }; benchmark( function(){ var n = N; while(--n) proc(); }, N + '-th proc with while loop' ); benchmark( function(){ var f = function(n){ proc(); if (--n) arguments.callee(n); }; f(N); }, N + '-th proc with higher-order function(argument.callee)' ); } main();