Skip to content

Commit

Permalink
Improve perf by removing keys array
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jan 18, 2024
1 parent acb8dfb commit f2f0991
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,32 +299,31 @@ export class Cache<T extends Container<any>> {
sheet: string[] = [];
changeId = 0;

private _keys: string[] = [];
private _children = Object.create(null) as Record<string, T | undefined>;
private _children: T[] = [];
private _counters = Object.create(null) as Record<string, number | undefined>;

constructor(public changes?: Changes) {}

add(style: T): void {
const id = style.id;
const count = this._counters[id] || 0;
const item = this._children[id] || (style.clone() as T);

this._counters[id] = count + 1;

if (count === 0) {
this._children[id] = item;
this._keys.push(id);
this.sheet.push(item.getStyles());
const item = style.clone() as T;
const index = this._children.push(item);
this.sheet.push(style.getStyles());
this.changeId++;
if (this.changes) this.changes.add(item, this._keys.length - 1);
} else if (item instanceof Cache && style instanceof Cache) {
if (this.changes) this.changes.add(item, index);
} else if (style instanceof Cache) {
const index = this._children.findIndex((x) => x.id === id);
const item = (this._children[index]! as unknown) as T & Cache<any>;
const prevItemChangeId = item.changeId;

item.merge(style);

if (item.changeId !== prevItemChangeId) {
const index = this._keys.indexOf(id);
this.sheet[index] = item.getStyles();
this.changeId++;
if (this.changes) this.changes.change(item, index, index);
Expand All @@ -339,14 +338,12 @@ export class Cache<T extends Container<any>> {
if (count) {
this._counters[id] = count - 1;

const item = this._children[id]!;
const index = this._keys.indexOf(id);
const index = this._children.findIndex((x) => x.id === id);
const item = this._children[index]!;

if (count === 1) {
delete this._counters[id];
delete this._children[id];

this._keys.splice(index, 1);
this._children.splice(index, 1);
this.sheet.splice(index, 1);
this.changeId++;
if (this.changes) this.changes.remove(item, index);
Expand All @@ -365,7 +362,7 @@ export class Cache<T extends Container<any>> {
}

values(): T[] {
return this._keys.map((key) => this._children[key]!);
return this._children;
}

merge(cache: Cache<any>) {
Expand Down

0 comments on commit f2f0991

Please sign in to comment.