Created
October 16, 2013 13:53
-
-
Save ishiduca/7008105 to your computer and use it in GitHub Desktop.
空の配列への書き込みベンチマーク ref: http://qiita.com/ishiduca/items/20488079d3790d037738
This file contains hidden or 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 words = 'a b c d e f g h i j k l m n o p q r s t u v w x y z' | |
function not_set_length () { | |
var w = words.split(' ') | |
var ww = [] | |
while (w.length) { | |
ww.push(w.pop()) | |
} | |
return true | |
} | |
function set_length () { | |
var w = words.split(' ') | |
var ww = []; ww.length = w.length | |
var i = 0 | |
while (w.length) { | |
ww[i++] = w.pop() | |
} | |
return true | |
} | |
function maybe_most_slowly () { | |
var w = words.split(' ') | |
var ww = [] | |
while (w.length) { | |
ww.unshift(w.shift()) | |
} | |
return true | |
} | |
(new Benchmark.Suite) | |
.add('配列の長さを事前に指定する',function () { | |
set_length() | |
}) | |
.add('配列の長さを指定しない', function () { | |
not_set_length() | |
}) | |
.add('配列の長さを指定しない かつ unshift() で足す', function () { | |
maybe_most_slowly() | |
}) | |
.on('cycle', function (ev) { | |
console.log(String(ev.target)) | |
}) | |
.on('complete', function () { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')) | |
}) | |
.run({async: true}) |
This file contains hidden or 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
配列の長さを事前に指定する x 1,227,097 ops/sec ±1.26% (90 runs sampled) | |
配列の長さを指定しない x 1,141,372 ops/sec ±0.42% (96 runs sampled) | |
配列の長さを指定しない かつ unshift() で足す x 227,978 ops/sec ±0.47% (91 runs sampled) | |
Fastest is 配列の長さを事前に指定する |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment