pockettojs / pocketto

A CRUD tools that can easily manage your data offline with real time ability
https://pocketto.dev
MIT License
7 stars 2 forks source link

Propose to use Set ( ) instead of object for model._meta._dirty #5

Open WongYC-66 opened 1 day ago

WongYC-66 commented 1 day ago

Current modelInstance_meta_dirty use Object.

For performance point, says a model has 100 property. Is Set( ) going to be faster?

Array.some() would take O(N) or worse case 100 iterations to return .

// Current:

isDirty(attribute?: ModelKey<this>): boolean {
        if (attribute) return !!this._meta._dirty[attribute as string];
        // return this._meta._dirty whereas the boolean value is true
        return Object.keys(this._meta._dirty).some(key => this._meta._dirty[key]);  // O(N)
}

Consider this.

model_meta_dirty = new Set()        // add "dirty" attribute

isDirty(attribute?: ModelKey<this>): boolean {
       return this.meta_dirty.size !== 0 || this.meta_dirty.has(attribute)   // O(1)
}
kentng201 commented 1 day ago

Can, this improvement should be quite good, you may improve it

kentng201 commented 1 day ago

I think this one should be able to close it

WongYC-66 commented 16 hours ago

im still working on this, having some syntax error, will update here again