为什么流行音乐比移动快? [英] Why is pop faster than shift?

查看:151
本文介绍了为什么流行音乐比移动快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

道格拉斯·克罗克福德(Douglas Crockford)在 JavaScript:The Good Parts 中指出,转变通常比流行音乐慢得多。 jsPerf 确认这一点。有谁知道为什么会这样?从一个不成熟的角度来看,他们似乎做了几乎相同的事情。

解决方案

删除返回的项目而不重新 - 地址数组并使对它的所有引用无效, shift()需要移动整个数组; pop()可以简单地从长度中减去1。


Douglas Crockford, in JavaScript: The Good Parts, states that "shift is usually much slower than pop". jsPerf confirms this. Does anyone know why this is the case? From an unsophisticated point of view, they seem to be doing pretty much the same thing.

解决方案

To remove the returned item without re-addressing the array and invalidating all references to it, shift() requires moving the entire array around; pop() can simply subtract 1 from its length.

这篇关于为什么流行音乐比移动快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆