Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(model): add caching for data keys #271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Model<T> extends EventEmitter {
Query;
_database: Database;

_dataKeys: string[] = [];
dirty = false;

/**
* Model constructor.
*
Expand Down Expand Up @@ -82,6 +85,17 @@ class Model<T> extends EventEmitter {
Object.assign(_Document.prototype, schema.methods);
}

/**
* Returns the cached data keys.
*/
get dataKeys(): string[] {
if (this.dirty) {
this._dataKeys = Object.keys(this.data);
this.dirty = false;
}
return this._dataKeys;
}

/**
* Creates a new document.
*
Expand Down Expand Up @@ -168,6 +182,7 @@ class Model<T> extends EventEmitter {
// Pre-hooks
return execHooks(schema, 'pre', 'save', data).then(data => {
// Insert data
this.dirty = true;
this.data[id] = result;
this.length++;

Expand Down Expand Up @@ -258,6 +273,7 @@ class Model<T> extends EventEmitter {
// Pre-hooks
return execHooks(schema, 'pre', 'save', doc).then(data => {
// Update data
this.dirty = true;
this.data[id] = result;

this.emit('update', data);
Expand Down Expand Up @@ -319,6 +335,7 @@ class Model<T> extends EventEmitter {
// Pre-hooks
return execHooks(schema, 'pre', 'save', data).then(data => {
// Replace data
this.dirty = true;
this.data[id] = result;

this.emit('update', data);
Expand Down Expand Up @@ -369,6 +386,7 @@ class Model<T> extends EventEmitter {
// Pre-hooks
return execHooks(schema, 'pre', 'remove', data).then(data => {
// Remove data
this.dirty = true;
this.data[id] = null;
this.length--;

Expand Down Expand Up @@ -422,7 +440,7 @@ class Model<T> extends EventEmitter {
* @param {object} [options] See {@link Model#findById}.
*/
forEach(iterator: (value: any, index: number) => void, options?: Options): void {
const keys = Object.keys(this.data);
const keys = this.dataKeys;
let num = 0;

for (let i = 0, len = keys.length; i < len; i++) {
Expand Down Expand Up @@ -461,7 +479,7 @@ class Model<T> extends EventEmitter {
find(query: object, options: Options): Query<T> | T[];
find(query: object, options: Options = {}): Query<T> | T[] {
const filter = this.schema._execQuery(query);
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const len = keys.length;
let limit = options.limit || this.length;
let skip = options.skip;
Expand Down Expand Up @@ -528,7 +546,7 @@ class Model<T> extends EventEmitter {
eq(i_: number, options?: Options): Document<T> | Record<PropertyKey, any> {
let index = i_ < 0 ? this.length + i_ : i_;
const data = this.data;
const keys = Object.keys(data);
const keys = this.dataKeys;

for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
Expand Down Expand Up @@ -586,7 +604,7 @@ class Model<T> extends EventEmitter {
if (!len) return new this.Query([]);

const arr = new Array(len);
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const keysLen = keys.length;
let num = 0;

Expand Down Expand Up @@ -651,7 +669,7 @@ class Model<T> extends EventEmitter {
*/
map<T>(iterator: (value: any, index: number) => T, options?: Options): T[] {
const result = new Array(this.length);
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const len = keys.length;

for (let i = 0, num = 0; i < len; i++) {
Expand Down Expand Up @@ -747,7 +765,7 @@ class Model<T> extends EventEmitter {
* @return {Boolean}
*/
every(iterator: (value: any, index: number) => any): boolean {
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const len = keys.length;
let num = 0;

Expand All @@ -772,7 +790,7 @@ class Model<T> extends EventEmitter {
* @return {Boolean}
*/
some(iterator: (value: any, index: number) => any): boolean {
const keys = Object.keys(this.data);
const keys = this.dataKeys;
const len = keys.length;
let num = 0;

Expand Down Expand Up @@ -925,6 +943,7 @@ class Model<T> extends EventEmitter {

for (let i = 0; i < len; i++) {
const item = arr[i];
this.dirty = true;
data[item._id] = schema._parseDatabase(item) as T;
}

Expand All @@ -944,7 +963,7 @@ class Model<T> extends EventEmitter {
toJSON(): any[] {
const result = new Array(this.length);
const { data, schema } = this;
const keys = Object.keys(data);
const keys = this.dataKeys;
const { length } = keys;

for (let i = 0, num = 0; i < length; i++) {
Expand Down
Loading