Performance between multidimensional or object arrays in JavaScript

I need to load a good piece of data from my API, and I have a choice of format that I get. My question is about performance and choosing the fastest format for loading a query and reading it quickly in JavaScript.

I can have a two-dimensional array:

[0][0] = true;
[0][1] = false;
[1][2] = true;
[...]
etc etc..

Or I can have an array of objects:

[
{ x: 0, y: 0, data: true},
{ x: 0, y: 1, data: false},
{ x: 1, y: 2, data: true},
[...]
etc etc..
] 

I could not find any reference for this comparison for a GET request with a huge amount of data. If there is anything anywhere, I would love to read it!

The second part of the question is to read the data. I will have a loop that will have to get a value for each coordinate.

, , . , , ?

?

.

+4
1

GET , , . , , , , , 12 , - 20 . , 3/5 , , , , .

: . , , :

, / - / , , -, , , . , , , . Chrome 45.0.2454.101 32- 64- Windows 7, , true:

  • ,
  • / , ,

, , 225 ops/sec :

var sum = 0;
for (var x in obj) {
  sum += obj[x].payload;
}

, 13 620 / :

var sum = 0;
for (var x = 0; x < arr.length; ++x) {
  sum += arr[x].payload
}

, , 14,698 / , :

var sum = 0;
for (var x = 0; x < 10000; ++x) {
  sum += obj[x].payload
}

, , , , , , , .

+2

Source: https://habr.com/ru/post/1606932/


All Articles