From Javascript's kiss
目的:测试存取局部变量、对象属性、数组的耗时差异。
onload = function(){
var local = "supershafa;sdjfalsjdfalsdjflasjdfahskdfhaksdhfaksdfas";
var person = {
name: {
first: "sha",
last: "feng"
},
age: 24
};
var nums = [1, 2, 3, [4, 5]];
Watch.print("<h2>JavaScript Data Access Test</h2>");
Watch.print("<h3>Setter</h3>");
Watch.execByTimes(function(){
local = 2;
}, 100000, "Local variable");
Watch.execByTimes(function(){
person.age = 25;
}, 100000, "Local objects");
Watch.execByTimes(function(){
nums[0] = 6;
}, 100000, "Local array");
Watch.execByTimes(function(){
person.name.first = "super";
}, 100000, "Local objects+1");
Watch.execByTimes(function(){
nums[3][1] = 9;
}, 100000, "Local array+1");
Watch.print("<h3>Getter</h3>");
var l1, l2 = 25;
Watch.execByTimes(function(){
l1 = l2;
}, 100000, "Local variable");
Watch.execByTimes(function(){
l1 = person.age;
}, 100000, "Local objects");
Watch.execByTimes(function(){
l1 = nums[0];
}, 100000, "Local array");
Watch.execByTimes(function(){
l1 = person.name.first;
}, 100000, "Local objects+1");
Watch.execByTimes(function(){
l1 = nums[3][1];
}, 100000, "Local array+1");
Watch.report();
}