-1

I am working on a script in which i need some data based on a lot of timestamps.

Below is just an example

var timestampData1 = [1555486016,1555486017,1555486018...]; 
var timestampData2 = [1555486016,1555486017,1555486018...];

var data = [];
data[1] = [];
$.each(timestampData1,function(index,value) {
    data[1][value] = 1;
});
data[2] = [];
$.each(timestampData2,function(index,value) {
    data[2][value] = 1;
});
console.log(data);

The example above will output the following in the console

enter image description here

However if i examine the data in the console, i see a lot of empty sets counting from 0 up to the very last timestamp

enter image description here

So my question is:

Will javascript set all of these indexes, or is it simply an indication in the console? If not, i guess that it is very bad for performance, doing it like above ?

JPJens
  • 1,185
  • 1
  • 17
  • 39

1 Answers1

0

Yes your assumption is correct. If you use objects which have key/value pairs whis won't be a problem, however you will you array methods like push and filter.

Example:

var timestampData1 = [1555486016,1555486017,1555486018...]; 
var timestampData2 = [1555486016,1555486017,1555486018...];

var data = [];
//Make data[1] an object not array
data[1] = {};
$.each(timestampData1,function(index,value) {
    data[1][value] = 1;
});
//Make data[1] an object not array
data[2] = {};
$.each(timestampData2,function(index,value) {
    data[2][value] = 1;
});
console.log(data);

Output:

enter image description here

Wendelin
  • 2,354
  • 1
  • 11
  • 28