在飞机大战中 需要处理的数据是大量的 所以做到尽量的节省性能 所以自己封装了一个数据的仓库
众所周知 js的 对象就是一个哈希表 那么哈希表来进行存储的话 那么将会性能提高(至少在存储删除方面特别快)
class Store {
constructor(data) {
this.Store = data || {};
}
// 通过id获取单个数据
getId(id) {
return this.Store[id];
}
// 检测是否存在
hasOwnProperty(id) {
return this.Store.hasOwnProperty(id);
}
// 获取数据长度
getLength() {
return Object.keys(this.Store).length;
}
// 获取所有仓库
getStore() {
return this.Store;
}
// 通过id删除
removeStore(id) {
return delete this.Store[id];
}
// 通过id设置内容
setId(id, value) {
if (this.Store.hasOwnProperty(id)) {
return Object.assign(this.Store[id], value);
} else {
return (this.Store[id] = value);
}
}
// 设置整个仓库
setStore(value) {
this.Store = value;
}
// 清空仓库
clearStore() {
const keys = Object.keys(this.Store);
for (const index in keys) {
delete this.Store[keys[index]];
}
return true;
}
}
那么这样可以优化 增加和删除的速度 飞机大战中 需要存放子弹数据 敌机数据等 会进行频繁的删除添加 通过这种可以优化其中的性能
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。