1、使用数组的方法
var obj = {
list: [],
ids: 0,
start: function () {
this.ids++;
var _time = new Date().getTime();
this.list.push({ id: this.ids, time: _time });
return this.ids;
},
end: function (id) {
var arr=this.list.filter(function(item,index,arr){
if(item.id===id){
arr.splice(index,1);
return true
}
return false;
});
console.log(arr);
if(arr.length===0){
console.log("错误id");
return;
}
var time=new Date().getTime()-arr[0].time;
return time;
}
}
var id=obj.start();
for (var i = 0; i < 10000000; i++) {
}
var id1=obj.start();
for (var j = 0; j < 10000000; j++) {
}
console.log(obj.end(id1));
console.log(obj.end(id));
2、使用对象的方法
var obj = {
data:{},
ids: 0,
start: function () {
this.ids++;
this.data[this.ids]=new Date().getTime();
return this.ids;
},
end: function (id) {
var item=this.data[id];
if(item===undefined){
console.log("id错误");
return;
}
delete this.data[id];
return new Date().getTime()-item;
}
}
var id=obj.start();
for (var i = 0; i < 10000000; i++) {
}
var id1=obj.start();
for (var j = 0; j < 10000000; j++) {
}
console.log(obj.end(id1));
console.log(obj.end(id));