Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数组长度对读写数组成员的性能影响 #69

Open
leeenx opened this issue Sep 14, 2017 · 0 comments
Open

数组长度对读写数组成员的性能影响 #69

leeenx opened this issue Sep 14, 2017 · 0 comments

Comments

@leeenx
Copy link
Contributor

leeenx commented Sep 14, 2017

数组长度对访问数组成员的性能影响

理论上说,数组长度对访问数组成功的性能不会有任何影响,因为数组的访问是直接通过地址访问的跟数组本身长度没有关系。我写了一个测试用例:https://jsperf.com/diff-len-array-getter

下面是 chrome 的截图:
图示

下面是 safari 的截图:
图示

从数据上说明了,数组长度对访问数组成员不会造成性能上的影响。

数组长度对数组成员赋值的性能影响

理论上说,对数组长度也不应该影响到数组成员的赋值操作。同样的,我写了一个测试用例:https://jsperf.com/diff-len-array-setter/1

以下是 chrome 的截图:
图示

以下是 safari 的截图:
图示

从截图上的数据分析是符合理论上的说法的。也就是:数组长度对数组成员赋值不会造成性能上的影响。
个人觉得数组长度是对赋值操作产生了一个微量影响,从上面的运行结果可以看到 length == 100000 时,数字低了 4~5%。不过数字太小可以算做误差内吧。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant