我来自非计算机科学背景,正在寻找关于如何选择/评估/思考数据结构的某种理论指导。
目标是CouchDB文档的模式,并且能够一次检索数百万个文件并有效地解析其内容。
特别是,我希望能够扫描文档的键(日期字符串)。下面是一个(我很可靠)测试,看看效率差异:
即便是这个测试也证明哪个是最有效的。 如何在编译/解释JavaScript时(或者通常如何处理不同的数据结构)如何找到有关这些数据结构如何“工作”的更多信息?
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
以上的一个打印输出是:
S1: 32002
S2: 50062
S3: 25442