迭代大数组vs多个数组vs对象

时间:2016-01-19 20:56:28

标签: javascript arrays object

这三种情况之间是否存在明显的性能差异?

场景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 个答案:

答案 0 :(得分:1)

正如评论中提到的那样,场景1和场景3基本相同(唯一的区别是你从一个对象中获取数组。从对象中检索数组是{ {3}}所以没有伤害)