为什么javascript中的'删除'速度慢? [英] Why is 'delete' slow in javascript?

查看:119
本文介绍了为什么javascript中的'删除'速度慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了这个jsperf结果: http://jsperf.com/delet-is-slow

I just stumbled upon this jsperf result: http://jsperf.com/delet-is-slow

它显示在javascript中使用 delete 的速度很慢,但我不确定为什么会这样。为了让事情变得缓慢,javascript引擎在幕后做什么?

It shows that using delete is slow in javascript but I am not sure I get why. What is the javascript engine doing behind the scene to make things slow?

推荐答案

我认为问题不在于删除速度慢的原因。 ..简单删除操作的速度不值得测量...

I think the question is not why delete is slow... The speed of a simple delete operation is not worth measuring...

您显示的JS perf链接执行以下操作:

The JS perf link that you show does the following:


  • 创建两个包含6个元素的数组。

  • 删除一个数组的一个索引。

  • 遍历每个数组的所有索引。

脚本显示迭代应用了删除的数组o通过正常数组迭代比慢。

The script shows that iterating through an array o which delete was applied is slower than iterating though a normal array.

您应该问问自己,为什么删除会使数组变慢?

You should ask yourself, why delete makes an array slow?

引擎内部存储连续的数组元素内存空间,并使用数字索引器访问它们。

The engine internally stores array elements in contiguous memory space, and access them using an numeric indexer.

这就是他们所谓的快速访问阵列。

That's what they call a fast access array.

如果删除此有序和连续索引中的一个元素,则强制数组变为字典模式...因此,之前的确切位置是数组中的项(索引器)成为字典中必须搜索元素的字典中的键。

If you delete one of the elements in this ordered and contiguous index, you force the array to mutate into dictionary mode... thus, what before was the exact location of the item in the array (the indexer) becomes the key in the dictionary under which the array has to search for the element.

因此迭代变慢,因为不继续进入内存中的下一个空间,但是你一遍又一遍地执行哈希搜索。

So iterating becomes slow, because don't move into the next space in memory anymore, but you perform over and over again a hash search.

这篇关于为什么javascript中的'删除'速度慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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