2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScript(TypeScript)で map と for of どちらがいい?

Last updated at Posted at 2020-03-12

結論

パターンを増やしてループ回数をいろいろ試しました
どうも結果が安定しない感じでした

確実言えることは「配列から配列のmap処理」をするなら「map」を使え

それ以外はどうでもいい

for ofのほうが効率よいです

二度目

実行結果

$ ts-node test.ts
--------------------------------------
loop count: 100
loopforOf: 0.135ms
loopMap: 0.010ms
loopforOf-add: 0.018ms
loopMap-add: 0.009ms
loopforOf-copy: 0.023ms
loopMap-copy: 0.019ms
loopfor-copy: 0.017ms
--------------------------------------
loop count: 10000
loopforOf: 0.468ms
loopMap: 0.052ms
loopforOf-add: 0.350ms
loopMap-add: 0.049ms
loopforOf-copy: 0.723ms
loopMap-copy: 0.054ms
loopfor-copy: 0.756ms
--------------------------------------
loop count: 1000000
loopforOf: 2.164ms
loopMap: 9.416ms
loopforOf-add: 2.932ms
loopMap-add: 5.973ms
loopforOf-copy: 10.398ms
loopMap-copy: 5.869ms
loopfor-copy: 8.759ms
--------------------------------------
loop count: 100000000
loopforOf: 2782.037ms
loopMap: 2939.962ms
loopforOf-add: 2790.378ms
loopMap-add: 2954.378ms
loopforOf-copy: 12903.242ms
loopMap-copy: 2940.156ms
loopfor-copy: 7907.237ms
--------------------------------------
loop count: 100
loopforOf: 0.008ms
loopMap: 0.008ms
loopforOf-add: 0.008ms
loopMap-add: 0.006ms
loopforOf-copy: 0.015ms
loopMap-copy: 0.009ms
loopfor-copy: 0.018ms
--------------------------------------
loop count: 10000
loopforOf: 0.147ms
loopMap: 0.118ms
loopforOf-add: 0.168ms
loopMap-add: 0.072ms
loopforOf-copy: 0.335ms
loopMap-copy: 0.073ms
loopfor-copy: 0.330ms
--------------------------------------
loop count: 1000000
loopforOf: 13.085ms
loopMap: 6.307ms
loopforOf-add: 13.312ms
loopMap-add: 6.267ms
loopforOf-copy: 31.049ms
loopMap-copy: 5.851ms
loopfor-copy: 31.188ms
--------------------------------------
loop count: 100000000
loopforOf: 2637.213ms
loopMap: 2791.229ms
loopforOf-add: 2643.046ms
loopMap-add: 2789.886ms
loopforOf-copy: 12128.398ms
loopMap-copy: 2786.353ms
loopfor-copy: 7179.958ms

ソースコード

function loopMap(a: number[]) {
  console.time("loopMap");
  a.map(_ => { });
  console.timeEnd("loopMap");
}
function loopMapAdd(a: number[]) {
  console.time("loopMap-add");
  let i = 0;
  a.map(_ => { i = i + 1; });
  console.timeEnd("loopMap-add");
}
function loopMapCopy(a: number[]) {
  console.time("loopMap-copy");
  const b = a.map(_ => i);
  console.timeEnd("loopMap-copy");
}

function loopforOf(a: number[]) {

  console.time("loopforOf");
  for (const i of a) { }
  console.timeEnd("loopforOf");
}
function loopforOfAdd(a: number[]) {
  console.time("loopforOf-add");
  let i = 0;
  for (const j of a) { i = i + 1; }
  console.timeEnd("loopforOf-add");
}
function loopforOfCopy(a: number[]) {
  console.time("loopforOf-copy");
  const d = Array<number>(a.length);
  let i = 0;
  for (const j of a) { d[i] = j; }
  console.timeEnd("loopforOf-copy");
}

function loopforCopy(a: number[]) {
  console.time("loopfor-copy");
  const d = Array<number>(a.length);
  let l = a.length;
  for (let i = 0; i < l; i++) { d[i] = a[i]; }
  console.timeEnd("loopfor-copy");
}

testLoop(100);
testLoop(10000);
testLoop(1000000);
testLoop(100000000);

testLoop(100);
testLoop(10000);
testLoop(1000000);
testLoop(100000000);

function testLoop(n) {
  console.log("--------------------------------------");
  console.log(`loop count: ${n}`)
  loopforOf(Array<number>(n));
  loopMap(Array<number>(n));
  loopforOfAdd(Array<number>(n));
  loopMapAdd(Array<number>(n));
  loopforOfCopy(Array<number>(n));
  loopMapCopy(Array<number>(n));
  loopforCopy(Array<number>(n));
}

一度目

ソースコード

function loopMap(a: number[]) {
  console.time("loopMap");
  a.map(_ => { });
  console.timeEnd("loopMap");
}

function loopforOf(a: number[]) {
  console.time("loopforOf");
  for (const i of a) { }
  console.timeEnd("loopforOf");
}
loopforOf(Array<number>(1000000));
loopMap(Array<number>(1000000));
loopforOf(Array<number>(1000000));
loopMap(Array<number>(1000000));

実行結果

# ts-node test.ts
loopforOf: 2.313ms
loopMap: 8.350ms
loopforOf: 1.895ms
loopMap: 5.925ms
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?