Last active
September 30, 2015 21:47
-
-
Save yukidarake/1866998 to your computer and use it in GitHub Desktop.
node.jsで最適なパフォーマンスを得るための書き方を測定
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require('benchmark'); | |
var options = { | |
onStart : function() { | |
console.log('h1. ' + this.name + 'で一番速いのは?'); | |
}, | |
onCycle : function(event, bench) { | |
console.log('* ' + bench.name + '\t' + bench.hz.toFixed(bench.hz < 100 ? 2 : 0)); | |
}, | |
onComplete : function() { | |
console.log('¥n{code}結論:' | |
+ this.filter('fastest').pluck('name').map(function(item) { | |
return '「' + item + '」'; | |
}).join('と') + '{code}¥n' | |
); | |
} | |
}; | |
(function() { | |
new Benchmark.Suite('文字列連結', options) | |
.add('+', function() { | |
var str = '<div>' | |
+ '<form action="http://localhost/" method="post">' | |
+ '</form>' | |
+ '</div>'; | |
}) | |
.add('+=', function() { | |
var str = '<div>'; | |
str += '<form action="http://localhost/" method="post">'; | |
str += '</form>'; | |
str += '</div>'; | |
}) | |
.add('String.concat', function() { | |
var str = '<div>' | |
.concat('<form action="http://localhost/" method="post">') | |
.concat('</form>') | |
.concat('</div>'); | |
}) | |
.add('Array.join', function() { | |
var str = [ | |
'<div>', | |
'<form action="http://localhost/" method="post">', | |
'</form>', | |
'</div>' | |
].join(''); | |
}) | |
.run(); | |
})(); | |
(function() { | |
var cachedRegExp = /o/; | |
new Benchmark.Suite('文字列マッチング', options) | |
.add('RegExp.test()', function() { | |
if(/o/.test('Hello World!')) {} | |
}) | |
.add('cached RegExp.test()', function() { | |
if(cachedRegExp.test('Hello World!')) {} | |
}) | |
.add('String.match()', function() { | |
if ('Hello World!'.match(/o/)) {} | |
}) | |
.add('cached String.match()', function() { | |
if ('Hello World!'.match(cachedRegExp)) {} | |
}) | |
.add('RegExp.exec()', function() { | |
if (/o/.exec('Hello World!')) {} | |
}) | |
.add('cached RegExp.exec()', function() { | |
if (cachedRegExp.exec('Hello World!')) {} | |
}) | |
.add('String.indexOf()', function() { | |
if ('Hello World!'.indexOf('o') >= 0) {} | |
}) | |
.run(); | |
})(); | |
(function() { | |
var times = 1000; | |
new Benchmark.Suite('配列へ要素を追加する書き方', options) | |
.add('Array.push()', function() { | |
var ary = []; | |
for (var i = 0; i < times; i++) { | |
ary.push(i); | |
} | |
}) | |
.add('ary[ary.length]に代入する書き方', function() { | |
var ary = []; | |
for (var i = 0; i < times; i++) { | |
ary[ary.length] = i; | |
} | |
}) | |
.run(); | |
})(); | |
(function() { | |
var ary = (function() { | |
var ary = []; | |
for (var i = 0; i < 1000; i++) { | |
ary.push(i + ''); | |
} | |
return ary; | |
})(); | |
new Benchmark.Suite('for文の書き方', options) | |
.add('for(毎回ary.lengthを確認)', function() { | |
for (var i = 0; i < ary.length; i++) { | |
ary[i] + ''; | |
} | |
}) | |
.add('for(最初にlにlengthを格納)', function() { | |
for (var i = 0, l = ary.length; i < l; i++) { | |
ary[i] + ''; | |
} | |
}) | |
.add('Array.forEach()', function() { | |
ary.forEach(function(item, i) { | |
if (item){} | |
}); | |
}) | |
.run(); | |
})(); | |
(function() { | |
var obj = { | |
callback : function() { | |
return 1000 * 1000; | |
} | |
}; | |
new Benchmark.Suite('if文', options) | |
.add('if文', function() { | |
if (obj && obj.callback) { | |
obj.callback(); | |
} | |
}) | |
.add('callback && callback()のような書き方', function() { | |
obj && obj.callback && obj.callback(); | |
}) | |
.run(); | |
})(); | |
(function() { | |
new Benchmark.Suite('真偽値へのキャスト', options) | |
.add('"!!"', function() { | |
if (!!null) {} | |
if (!!undefined) {} | |
if (!!'') {} | |
if (!!0) {} | |
}) | |
.add('Bool', function() { | |
if (Boolean(null)) {} | |
if (Boolean(undefined)) {} | |
if (Boolean('')) {} | |
if (Boolean(0)) {} | |
}) | |
.run(); | |
})(); | |
(function() { | |
new Benchmark.Suite('文字列へのキャスト', options) | |
.add("+ ''", function() { | |
'' + ''; | |
0 + ''; | |
true + ''; | |
}) | |
.add('String', function() { | |
String(''); | |
String(0); | |
String(true); | |
}) | |
.run(); | |
})(); | |
(function() { | |
new Benchmark.Suite('数値へのキャスト', options) | |
.add('+', function() { | |
+null; | |
+undefined; | |
+''; | |
+"0"; | |
}) | |
.add('Number', function() { | |
Number(null); | |
Number(undefined); | |
Number(''); | |
Number(0); | |
}) | |
.run(); | |
})(); | |
(function() { | |
var obj = { | |
prop1 : true, | |
prop2 : false, | |
prop3 : undefined, | |
prop4 : 0 | |
}; | |
new Benchmark.Suite('プロパティがオブジェクトに存在するかどうか', options) | |
.add('"."', function() { | |
if (obj.prop1) {} | |
if (obj.prop2) {} | |
if (obj.prop3) {} | |
if (obj.prop4) {} | |
if (obj.prop5) {} | |
}) | |
.add('"in"', function() { | |
if ('prop1' in obj) {} | |
if ('prop2' in obj) {} | |
if ('prop3' in obj) {} | |
if ('prop4' in obj) {} | |
if ('prop5' in obj) {} | |
}) | |
.add('Object.hasOwnProperty()', function() { | |
if (obj.hasOwnProperty('prop1')) {} | |
if (obj.hasOwnProperty('prop2')) {} | |
if (obj.hasOwnProperty('prop3')) {} | |
if (obj.hasOwnProperty('prop4')) {} | |
if (obj.hasOwnProperty('prop5')) {} | |
}) | |
.run(); | |
})(); | |
(function() { | |
new Benchmark.Suite('現在のUNIXタイムの取得', options) | |
.add('new Date().getTime()', function() { | |
new Date().getTime(); | |
}) | |
.add('Date.now()', function() { | |
Date.now(); | |
}) | |
.run(); | |
})(); | |
(function() { | |
var Test1 = function(name) { | |
setTimeout(function() { | |
this.name = name; | |
}.bind(this), 10); | |
}; | |
var Test2 = function(name) { | |
var self = this; | |
setTimeout(function() { | |
self.name = name; | |
}, 10); | |
}; | |
new Benchmark.Suite('thisをbindする方法', options) | |
.add('Function.bind()', function() { | |
var test = new Test1('test'); | |
}) | |
.add('var self = this;', function() { | |
var test = new Test2('test'); | |
}) | |
.run(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require('benchmark'); | |
var options = { | |
onStart: function() { | |
console.log(this.name + 'で速いのはどちら?'); | |
}, | |
onCycle: function(event) { | |
console.log(String(event.target)); | |
}, | |
onComplete: function() { | |
this.filter('fastest').pluck('name').map(function(item) { | |
return '「' + item + '」'; | |
}).join('と') + '¥n'; | |
} | |
}; | |
(function() { | |
var len = 100000; | |
new Benchmark.Suite('Array操作', options) | |
.add('unshift', function() { | |
var list = []; | |
for (var i = 0; i < len; i++) { | |
list.unshift(i + ''); | |
} | |
}) | |
.add('push', function() { | |
var list = []; | |
for (var i = 0; i < len; i++) { | |
list.push(i + ''); | |
} | |
}) | |
.run(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment