I come from a non-computer science background, and am looking for some kind of theoretical guidance in how to choose/evaluate/think about data structures.
The immediate aim is a schema for CouchDB documents and be able to retrieve millions back at a time and parse their contents efficiently.
In particular, I want to be able to scan a document's keys (which are date strings). Below is a (poor I'm sure) test to see efficiency differences between:
- Iterating through object keys, checking if they are dates and if they are parsing the dates
- Iterating through an array within an abject where you KNOW all the values are dates, and parsing the dates
- Accessing dates by an explicitly named 'date' attribute.
Even this test makes it evident which is the most efficient. How can I find out more about how these data structures 'work' when compiling/interpreting JavaScript (or just how different data structures are handled in general)?
var s1 = {
x: 1,
"10 March 2016": "2",
"11 March 2016": "asdfasdf",
"12 March 2016": "asdfasdfas"
};
s1 = JSON.stringify(s1);
var s2 = {
x: 1,
readings: [
["10 March 2016", "asdf"],
["10 March 2016", "asdf"],
["10 March 2016", "asdf"]
]
};
s2 = JSON.stringify(s2);
var s3 = {
x: 1,
readings: [{
date: "10 March 2016",
reading: "asdfasd"
}, {
date: "10 March 2016",
reading: "asdfasd"
}, {
date: "10 March 2016",
reading: "asdfasd"
}]
};
s3 = JSON.stringify(s3);
var iterations = 9000000;
// Testing s1
var t1 = new Date();
for (var i = 0; i < iterations; i++) {
var t1o = JSON.parse(s1);
for (var k in t1o) {
if(new Date(k)) {
var d = t1o[k];
}
}
}
var t2 = new Date();
console.log("S1: ", t2 - t1); // 2nd fastest
// Testing s2
var t1 = new Date();
for (var i = 0; i < iterations; i++) {
var t1o = JSON.parse(s2);
for (var k in t1o.readings) {
if(new Date(k)) {
var d = t1o.readings[k];
}
}
}
var t2 = new Date();
console.log("S2: ", t2 - t1); // slowest
// Testing s3
var t1 = new Date();
for (var i = 0; i < iterations; i++) {
var t1o = JSON.parse(s3);
for (var j = 0; j < t1o.readings.length; j++) {
var d = new Date(t1o.readings[j].date);
};
}
var t2 = new Date();
console.log("S3: ", t2 - t1); // fastest
One printout for the above is:
S1: 32002
S2: 50062
S3: 25442