我必须将一些事件记录到一个 json 对象中,那么从性能的角度来看,长的还是许多子对象更好呢?
为了更好地解释,看看这两个对象:
第一个(长物体)
var eventsObj = {
target: {
timestamp1: window,
timestamp2: document,
timestamp3: document
},
keyCode: {
timestamp1: 0,
timestamp2: 66,
timestamp3: 67
},
mouseX: {
timestamp1: 1,
timestamp2: 2,
timestamp3: 3
},
mouseY: {
timestamp1: 0,
timestamp2: 1,
timestamp3: 2
}
...etc...
};
第二(许多对象)
var eventsObj = {
timestamp1: {
target: window,
keyCode: 0,
mouseX: 1,
mouseY: 0
},
timestamp2: {
target: document,
keyCode: 66,
mouseX: 2,
mouseY: 1
},
timestamp3: {
target: document,
keyCode: 67,
mouseX: 3,
mouseY: 2
}
...etc...
};
更新
http://jsperf.com/object-performance-long-items-or-many-items