簡體   English   中英

對象鍵與數組查找性能

[英]Object key vs array lookup performance

示例 1:

["member1", "member2",...,..., "member100000"]

示例 2:

{
    "member1": true, // (doesn't really need values, only keys :/)
    "member2": true,
    "...",
    "member100000": true
}

我將成員存儲在每個內容的數組中,就像示例 1 一樣,但是這樣做我必須遍歷數組中的 49999 個項目,才能找到成員 50000,所以我想簡單地檢查一下在 javascript 對象中定義特定的鍵在這里是一個更好的方法,雖然我不需要存儲值,但只檢查鍵是否未定義?

我需要的是能夠檢查例如。 “member50000”作為數組中的值存在 - 或作為對象中的鍵存在。

我做了一些基准測試,但我不確定我得出了正確的結論,或者我在比較中做錯了什么: http : //jsperf.com/lolda123

根據上述測試結果,是否可以得出將鍵/值對保存在值為布爾值 (true) 的對象中並執行if(obj["member50000"])是性能最佳選項的結論是否公平? 即使不存在具有給定鍵的屬性? 正如我所看到的,根據我的測試結果,檢查密鑰本身是否存在,在性能方面似乎要昂貴得多,但檢查密鑰是否存在,確實是我所需要的。

我不在乎價值,所以我在這里遺漏了什么,或者為什么更好的解決方案似乎是你通過鍵查找值的方法,而不是僅僅在對象內部查找鍵?

好吧,我認為同樣的事情,使用 Object 會更快,我證明自己錯了!

我在 NodeJS 6.3(V8 引擎)上使用與 jsPerf 相同的堆棧運行性能測試:

https://github.com/amoldavsky/js-array-indexof-vs-hashmap

結果是:

  • 在較大的數據集上查找要快得多
  • 創建對象數據結構(基本上是 HashMap )是昂貴的! 並且比使用數組慢得多
  • 當結合插入和查找時,array.indexOf 的速度要快得多

如果您認為我在任何測試中犯了錯誤,請隨時提出來,我們可以重新測試。 雖然到目前為止看起來 array.indexOf 快得多。

obj.hasOwnPropertyarr.indexOf http://jsperf.com/array-hasownproperty-vs-array-indexof快得多

使用arr.indexOf也應該比任何循環都要快。

對此很好奇,因為Object.prototype是任何數組實例的祖先,因此它必須比對象更大,並且在屬性查找方面更慢。

在我的測試中,我將字符串列表存儲為數組元素和對象鍵。 然后我測量檢查對象和數組實現中每個鍵是否存在所需的時間。 重復 100000 次。

通常,對象碰巧更快。 Null 對象在 Chromium 上明顯更快。

{
  const iterations = 1000000;

  let arrTotal = 0;
  let objTotal = 0;
  let nullObjTotal = 0;

  let a = '';

  let keys = [
    "The standard Lorem Ipsum passage",
    "used since the 1500sLorem ipsum dolor sit amet",
    "consectetur adipiscing elit",
    "Ut enim ad minim veniam",
    "Excepteur sint occaecat cupidatat non proident",
    "sunt in culpa qui officia deserunt mollit anim id est laborum",
    "Section 1",
    "32 of de Finibus Bonorum et Malorum",
    "totam rem aperiam",
    "Neque porro quisquam est",
    "qui dolorem ipsum quia dolor sit amet",
    "consectetur",
    "adipisci velit",
    "Ut enim ad minima veniam",
    "the master-builder of human happiness",
    "No one rejects",
    "dislikes",
    "or avoids pleasure itself",
    "because it is pleasure",
    "because it is pain",
    "To take a trivial example",
    "which of us ever undertakes laborious physical exercise",
    "33 of de Finibus Bonorum et Malorum",
    "similique sunt in culpa qui officia deserunt mollitia animi",
    "id est laborum et dolorum fuga",
    "Et harum quidem rerum facilis est et expedita distinctio",
    "Nam libero tempore",
    "omnis voluptas assumenda est",
    "omnis dolor repellendus",
    "Itaque earum rerum hic tenetur a sapiente delectus",
    "1914 translation by H",
    "RackhamOn the other hand",
    "so blinded by desire",
    "These cases are perfectly simple and easy to distinguish",
    "In a free hour",
    "every pleasure is to be welcomed and every pain avoided",
    "or else he endures pains to avoid worse pains"
  ];

  let nullObj = Object.create(null);
  for (let key of keys) nullObj[key] = null;

  let obj = {
    "The standard Lorem Ipsum passage": null,
    "used since the 1500sLorem ipsum dolor sit amet": null,
    "consectetur adipiscing elit": null,
    "Ut enim ad minim veniam": null,
    "Excepteur sint occaecat cupidatat non proident": null,
    "sunt in culpa qui officia deserunt mollit anim id est laborum": null,
    "Section 1": null,
    "32 of de Finibus Bonorum et Malorum": null,
    "totam rem aperiam": null,
    "Neque porro quisquam est": null,
    "qui dolorem ipsum quia dolor sit amet": null,
    "consectetur": null,
    "adipisci velit": null,
    "Ut enim ad minima veniam": null,
    "the master-builder of human happiness": null,
    "No one rejects": null,
    "dislikes": null,
    "or avoids pleasure itself": null,
    "because it is pleasure": null,
    "because it is pain": null,
    "To take a trivial example": null,
    "which of us ever undertakes laborious physical exercise": null,
    "33 of de Finibus Bonorum et Malorum": null,
    "similique sunt in culpa qui officia deserunt mollitia animi": null,
    "id est laborum et dolorum fuga": null,
    "Et harum quidem rerum facilis est et expedita distinctio": null,
    "Nam libero tempore": null,
    "omnis voluptas assumenda est": null,
    "omnis dolor repellendus": null,
    "Itaque earum rerum hic tenetur a sapiente delectus": null,
    "1914 translation by H": null,
    "RackhamOn the other hand": null,
    "so blinded by desire": null,
    "These cases are perfectly simple and easy to distinguish": null,
    "In a free hour": null,
    "every pleasure is to be welcomed and every pain avoided": null,
    "or else he endures pains to avoid worse pains": null
  };

  let arr = [
    "The standard Lorem Ipsum passage",
    "used since the 1500sLorem ipsum dolor sit amet",
    "consectetur adipiscing elit",
    "Ut enim ad minim veniam",
    "Excepteur sint occaecat cupidatat non proident",
    "sunt in culpa qui officia deserunt mollit anim id est laborum",
    "Section 1",
    "32 of de Finibus Bonorum et Malorum",
    "totam rem aperiam",
    "Neque porro quisquam est",
    "qui dolorem ipsum quia dolor sit amet",
    "consectetur",
    "adipisci velit",
    "Ut enim ad minima veniam",
    "the master-builder of human happiness",
    "No one rejects",
    "dislikes",
    "or avoids pleasure itself",
    "because it is pleasure",
    "because it is pain",
    "To take a trivial example",
    "which of us ever undertakes laborious physical exercise",
    "33 of de Finibus Bonorum et Malorum",
    "similique sunt in culpa qui officia deserunt mollitia animi",
    "id est laborum et dolorum fuga",
    "Et harum quidem rerum facilis est et expedita distinctio",
    "Nam libero tempore",
    "omnis voluptas assumenda est",
    "omnis dolor repellendus",
    "Itaque earum rerum hic tenetur a sapiente delectus",
    "1914 translation by H",
    "RackhamOn the other hand",
    "so blinded by desire",
    "These cases are perfectly simple and easy to distinguish",
    "In a free hour",
    "every pleasure is to be welcomed and every pain avoided",
    "or else he endures pains to avoid worse pains"
  ];

  for (let i = 0, stamp = 0, length = keys.length; i < iterations; ++i) {
    stamp = performance.now();
    for (let j = 0; j < length; ++j) {
      if (keys[j] in obj) a = keys[j];
    }
    objTotal += (performance.now() - stamp)/1000;

    stamp = performance.now();
    for (let j = 0; j < length; ++j) {
      if (~arr.indexOf(keys[j])) a = keys[j];
    }
    arrTotal += (performance.now() - stamp)/1000;

    stamp = performance.now();
    for (let j = 0; j < length; ++j) {
      if (keys[j] in nullObj) a = keys[j];
    }
    nullObjTotal += (performance.now() - stamp)/1000;
  }

  console.log(`Array total: ${arrTotal}; Array avarage: ${arrTotal/iterations}(s).`);
  console.log(`Object total: ${objTotal}; Object avarage: ${objTotal/iterations}(s).`);
  console.log(`Null object total: ${nullObjTotal}; Null object avarage: ${nullObjTotal/iterations}(s).`);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM