ES6飞机大战篇-数据存储的封装(object)

在飞机大战中 需要处理的数据是大量的 所以做到尽量的节省性能 所以自己封装了一个数据的仓库
众所周知 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;
  }
}

那么这样可以优化 增加和删除的速度 飞机大战中 需要存放子弹数据 敌机数据等 会进行频繁的删除添加 通过这种可以优化其中的性能

上一篇:分布式锁之Redis实现


下一篇:【Redisson】二.可重入锁-lua脚本加锁逻辑源码