js排序的时间复杂度_javascript的array.indexOf的时间复杂度是多少?

找到匹配未排序数组中的值的第一个索引的最有效的方法是按顺序遍历列表,即O(n).

MDN还有一些提示:

Returns the first index at which a given element can be found in the array, or -1 if it is not present.

[…]

fromIndex

Default: 0 (Entire array is searched)

The index to start the search at. If the index is greater than or equal to the array’s length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched.

EncodedJSValue JSC_HOST_CALL arrayProtoFuncIndexOf(ExecState* exec)

{

// 15.4.4.14

JSObject* thisObj = exec->hostThisValue().toThis(exec, StrictMode).toObject(exec);

unsigned length = thisObj->get(exec, exec->propertyNames().length).toUInt32(exec);

if (exec->hadException())

return JSValue::encode(jsUndefined());

unsigned index = argumentClampedIndexFromStartOrEnd(exec, 1, length);

JSValue searchElement = exec->argument(0);

for (; index < length; ++index) {

JSValue e = getProperty(exec, thisObj, index);

if (exec->hadException())

return JSValue::encode(jsUndefined());

if (!e)

continue;

if (JSValue::strictEqual(exec, searchElement, e))

return JSValue::encode(jsNumber(index));

}

return JSValue::encode(jsNumber(-1));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值