Nurbol Alpysbayev
Nurbol Alpysbayev

Reputation: 21881

Does an array with n huge object elements sort slower that an array with n tiny object elements?

Does the size of an array object element affect the performance of sorting the array?

I suspect the answer is no, because only memory pointers (if I get it correctly) are sorted. But I would be grateful if someone who knows will confirm/reject/explain this.

The property by which to sort contains a number: 1,2,3 etc..

Example of the two arrays:

const arr1 = [{blob: {/*huge object here*/}, order: 1}, /*similar objects*/]

const arr2 = [{smallObject: {foo: 'bar'}, order: 1}, /*similar objects*/]

// sorting like this:
arr.sort((a,b)=> a.order > b.order ? 1 : -1)

Upvotes: 1

Views: 230

Answers (2)

gilamran
gilamran

Reputation: 7294

I've created a benchmark here: http://jsben.ch/vSVZa

enter image description here

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138267

No. Pointers are the only way to represent dynamic nested structures efficiently, so the "size" of objects won't affect the sorting, as just pointers have to get swapped.

The size of the array however does affect the sorting speed, if there are more elements you have to sort more.

Upvotes: 3

Related Questions