找到陣列中某個元素方法效能比較

2021-09-24 17:42:17 字數 777 閱讀 4282

const arr = 

for(let i=0; i<1000000; i++) `)

}console.time('indexof')

arr.indexof('ahdgd10000')

console.timeend('indexof')

// indexof: 0.0400390625ms

console.time('includes')

arr.includes('ahdgd10000')

console.timeend('includes')

// includes: 0.025146484375ms

console.time('hasownproperty')

arr.hasownproperty('ahdgd10000')

console.timeend('hasownproperty')

// hasownproperty: 0.004150390625ms

通過上面實踐可以看出來arr.hasownproperty()方法要快的多。

不管是arr.indexof()還是es6的arr.includes(),本質上都是遍歷陣列

hasownproperty()方法會返回乙個布林值,指示物件自身屬性中是否具有指定的屬性(也就是是否有指定的鍵)該方法會忽略掉那些從原型鏈上繼承到的屬性。

陣列也是物件,陣列元素相當於以下標和對應的值存在的建值對。

JS 陣列求和方法與效能比較

function sum arr function sum arr return res function sum arr function sum arr function sum arr 執行效率 引數生成const oriarr array.from new array 100000 keys...

各種排序方法的效能比較

測試環境說明 win xp下,vs2008,主頻 core2 雙核2.53ghz 下面是測試的 using system using system.collections.generic using system.linq using system.text using system.collect...

分頁實現方法的效能比較

我們先給出幾種主要的分頁方法和核心語句,然後直接給出結論,有興趣的讀者可以看看後面的資料 幾種常用儲存過程分頁方法 topn方法 select top pagesize from tablename where id not in select top pageindex 1 pagesize id...