for inとObject.keysの件追記

http://d.hatena.ne.jp/hokaccha/20110729/1311927481
この件にuupaaさんからつっこみ。

https://twitter.com/uupaa/status/165692109565870080
http://uupaa.hatenablog.com/entry/2012/02/04/145400

Object.keysはスコープつくるので正確には等価じゃないというつっこみ。たしかに。んでObject.keysのほうもforでスコープつくらないでループしてベンチとった。

前のベンチマークにこれ足した。

.add('Object.keys (for)', function() {
  var keys = Object.keys(obj);
  var key, val;
  for (var i = 0, len = keys.length; i < len; i++) {
    key = keys[i];
    val = obj[key];
  }
})

結果。

# 1個
$ node bench.js 1
for in x 9,756,887 ops/sec ±0.29% (63 runs sampled)
for in (not call hasOwn) x 28,565,185 ops/sec ±0.15% (61 runs sampled)
Object.keys x 2,321,018 ops/sec ±0.26% (63 runs sampled)
Object.keys (for) x 2,747,046 ops/sec ±0.39% (63 runs sampled)

for in: 420%
for in (not call hasOwn): 1232%
Object.keys: 100%
Object.keys (for): 118%


# 1000個
$ node bench.js 1000
for in x 2,524 ops/sec ±2.96% (13 runs sampled)
for in (not call hasOwn) x 3,355 ops/sec ±0.32% (18 runs sampled)
Object.keys x 4,313 ops/sec ±1.10% (23 runs sampled)
Object.keys (for) x 4,751 ops/sec ±0.32% (63 runs sampled)

for in: 100%
for in (not call hasOwn): 136%
Object.keys: 174%
Object.keys (for): 193%


# 10000個
$ node bench.js 10000
for in x 17.40 ops/sec ±1.30% (28 runs sampled)
for in (not call hasOwn) x 353 ops/sec ±0.97% (61 runs sampled)
Object.keys x 488 ops/sec ±0.48% (64 runs sampled)
Object.keys (for) x 525 ops/sec ±1.70% (61 runs sampled)

for in: 100%
for in (not call hasOwn): 2037%
Object.keys: 2830%
Object.keys (for): 3004%

全体的にforEachより10%くらい早い感じ。