开发者

Why searching for the middle item in an array of 100 is faster than in an array of 10 items? [closed]

开发者 https://www.devze.com 2023-04-05 20:30 出处:网络
It's diffi开发者_运维知识库cult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current for
It's diffi开发者_运维知识库cult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I made some tests to check this but is quite not expected: http://jsperf.com/value-in-array-or-object

You can run those tests too..


It's not faster. It appeared faster in your tests because you were assigning rather than comparing in the tests for 50 and 100 items:

if(a100[x] = 'item50'){
    break;
}

The test with 10 items was correct:

if(a10[x] === 'item5'){
    break;
}

See updated test cases here.

0

精彩评论

暂无评论...
验证码 换一张
取 消