在 Actionscript 中连接数组的最佳方法
我需要将一个数组添加到另一个数组(只关心保存连接的数组)。哪种方法是首选?速度是首要考虑因素,其次是可读性(我认为选项 1 是一个更简洁的选项)。我认为它也可能取决于数组的长度,但是有什么好的指导方针吗?
选项 1:
var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");
// Don't care about array2 after this point.
var array1 = array1.concat(array2);
选项 2:
var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");
// Don't care about array2 after this loop has run.
for each(var item:Object in array2)
{
array1.push(item);
}
I need to add one array to another (only care about saving the joined one). Which is the preferred way of doing this? Speed is the primary concern followed by readability (I consider Option 1 to be a cleaner option). I assume it might also depend on the length of the arrays, but are there any good guidelines?
Option 1:
var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");
// Don't care about array2 after this point.
var array1 = array1.concat(array2);
Option 2:
var array1:Array = new Array("1","2","3");
var array2:Array = new Array("4","5","6");
// Don't care about array2 after this loop has run.
for each(var item:Object in array2)
{
array1.push(item);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来像是一项......基准测试的工作!
正如我所怀疑的, concat 速度要快得多,尤其是在处理较大的数组时。
输出
这些数字以毫秒为单位,因此正如 Richard 指出的那样,除非您的数组具有大量元素,或者您非常频繁地连接数组,否则这种优化不值得您时间
This sounds like a job for... benchmark!
As I would have suspected, concat is far faster, especially when dealing with larger arrays.
OUTPUT
Those numbers are in milliseconds, so as Richard points out, unless your arrays have a huge number of elements, or you are concatenating arrays very frequently, then this is an optimization that is not worth your time
我个人会使用 concat,因为它更简单。
它可能也更快(因为它可以本地实现),但如果它对您很重要,请衡量它。除非您正在处理大量的数组值,否则不太可能有差异,并且这将是微优化的一个主要示例。
I would use
concat
personally, because it's simpler.It's probably faster, too (because it can be natively implemented), but measure it if it's important to you. Unless you are dealing with an extremely large number of array values, there's unlikely to be a difference and would be a prime example of micro-optimisation.