0
0

More than 3 years have passed since last update.

JavaScriptでobjectとarrayのデータアクセス速度比較

Posted at

検証

// それぞれ要素を300個ずつ用意する
const arr = Array(300).fill(1);

const obj = {};
Array(300).fill(1).forEach((i, e) => obj[i] = e);

// ランダムにデータを参照する
function test() {
    const randomIds = Array(300).map(e => Math.floor(Math.random() * 299));

    console.time();
    randomIds.forEach(id => array.find((i, e) => i === id));
    console.timeEnd();

    console.time();
    randomIds.forEach(id => obj[id]);
    console.timeEnd();
}

結果

objectの方が高速。

0
0
1

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
0
0