Solo
Solo

Reputation: 6957

Iterating big array vs multiple arrays vs object

Is there any noticeable performance difference between these three scenarios?


Scenario 1: two arrays already sorted by condition

//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
}

Scenario 2: one big array, check condition in loop

//Single array: 50 000 items
var count = myArray.length;

for( a = 0; a < count; a++ ) {

    if( /* Condition is met */ ) {

        //Action 1
    }
    else {

        //Action 2
    }
}

Scenario 3: one object that contains two arrays - good for storing other data too

//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
}

Upvotes: 0

Views: 254

Answers (1)

James J. Hill
James J. Hill

Reputation: 139

As it's been mentioned in the comments, Scenario 1 and 3 are essentially the same (with the only difference is that you're obtaining the arrays from an object. Retrieving the array from the object is constant time so no damage there)

Upvotes: 1

Related Questions