这三种情况之间是否存在明显的性能差异?
<小时/>场景 1:两个数组已按条件排序
//Array 1: 25 000 items
var count = myFirstArray.length;
for( a = 0; a < count; a++ ) {
//Action 1
}
//Array 2: 25 000 items
var count = mySecondArray.length;
for( a = 0; a < count; a++ ) {
//Action 2
}
<小时/>
场景 2:一个大数组,在循环中检查条件
//Single array: 50 000 items
var count = myArray.length;
for( a = 0; a < count; a++ ) {
if( /* Condition is met */ ) {
//Action 1
}
else {
//Action 2
}
}
<小时/>
场景 3:一个对象包含两个数组 - 也适合存储其他数据
//Single object with arrays: each with 25 000 items
var firstCount = myObject['myFirstArray'].length;
var secondCount = myObject['mySecondArray'].length;
for( a = 0; a < firstCount; a++ ) {
//Action 1
}
for( a = 0; a < secondCount; a++ ) {
//Action 2
}
最佳答案
正如评论中提到的,场景 1 和 3 本质上是相同的(唯一的区别是您从对象获取数组。从对象检索数组是 constant time 所以没有损坏)
关于javascript - 迭代大数组与多个数组与对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34886780/