node.js v5.7.1で計測してみました、速度計測にはbenchmark.jsを使用しています。また、var
とはスコープが異なるlet
版も合わせて計測しました。
JavaScript
1"use strict";
2var Benchmark = require("benchmark");
3var object = "test";
4var times = 100000;
5
6function naka() {
7 var array = [];
8 for (var i = 0; i < times; i++) {
9 var hoge = object;
10 array.push(hoge);
11 }
12}
13
14function soto() {
15 var array = [];
16 var hoge;
17 for (var i = 0; i < times; i++) {
18 hoge = object;
19 array.push(hoge);
20 }
21}
22
23function naka_let() {
24 let array = [];
25 for (let i = 0; i < times; i++) {
26 let hoge = object;
27 array.push(hoge);
28 }
29}
30
31function soto_let() {
32 let array = [];
33 let hoge;
34 for (let i = 0; i < times; i++) {
35 hoge = object;
36 array.push(hoge);
37 }
38}
39
40(new Benchmark.Suite).add("naka", naka).add("soto", soto)
41 .add("naka_let", naka_let).add("soto_let", soto_let)
42 .on("cycle", event => console.log(String(event.target)))
43 .on("complete", function() {
44 console.log("Fastest is " + this.filter("fastest").map("name"));
45 })
46 .run({async: true});
結果
naka x 1,523 ops/sec ±1.11% (79 runs sampled)
soto x 1,556 ops/sec ±1.11% (82 runs sampled)
naka_let x 1,026 ops/sec ±1.40% (85 runs sampled)
soto_let x 1,011 ops/sec ±1.11% (82 runs sampled)
Fastest is soto
中か外はほとんど誤差です。パフォーマンスは変わらないと思って問題ないと思います。ECMAScript 2015から追加されたlet
はvar
より遅くなるようです。var
に比べてチューニングがまだ不十分なのかも知れません。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/03/11 00:04