You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
My team and I recently had big performance issues on our app. We managed to fix them after refactoring several collections management.
We usually write entity constructors to always have a consistent model, thus a container (in the following example the sea) is often created with a bunch a elements (fish) passed directly in the constructor. We then persist and flush the container alongside all elements in one call.
It leads to bad performance. Creating and persisting/flushing an empty container which will then be filled with elements is 10 times faster.
To Reproduce
Steps to reproduce the behavior:
Launch this snippet with jest and compare elapsed time. jest flush.spec.ts
/* eslint-disable max-classes-per-file, @typescript-eslint/no-use-before-define */import{Collection,Entity,ManyToOne,MikroORM,OneToMany,PrimaryKey}from'@mikro-orm/core';importtype{SqliteDriver}from'@mikro-orm/sqlite';
@Entity()classSea{
@PrimaryKey()id!: number;
@OneToMany(()=>Fish,({ sea })=>sea)fishes=newCollection<Fish>(this);}
@Entity()classFish{
@PrimaryKey()id!: number;
@ManyToOne(()=>Sea)sea!: Sea;}describe('MikroORM Collection flush issue',()=>{letorm: MikroORM<SqliteDriver>;constnumberOfFishInTheSea=10000;beforeEach(async()=>{orm=awaitMikroORM.init({entities: [Sea,Fish],dbName: ':memory:',type: 'sqlite',});awaitorm.getSchemaGenerator().createSchema();});afterEach(()=>orm.close(true));test('when persisting the whole model, it is slow',async()=>{constentityManager=orm.em.fork();// so hard to writeconstmediterranean=newSea();constgroupers=Array.from({length: numberOfFishInTheSea}).map(()=>newFish());mediterranean.fishes.add(groupers);// everything is ready let's persistawaitentityManager.persistAndFlush(mediterranean);});test('when flushing the container before the contained data, it is fast',async()=>{constentityManager=orm.em.fork();// still so hard to write, but still nothing to do with our problemconstmediterranean=newSea();// Let's persist our sea before adding fish. It speed up the final flush...awaitentityManager.persistAndFlush(mediterranean);constgroupers=Array.from({length: numberOfFishInTheSea}).map(()=>newFish());mediterranean.fishes.add(groupers);awaitentityManager.flush();});});
Expected behavior
I would expect similar performance in both case.
Additional context
Following old issue guided us towards the workaround: #2379
Is this a regression?
Versions
Dependency
Version
node
18.17.1
typescript
4.3.5
mikro-orm
5.8.0
mikro-orm/postgresql
5.8.0
mikro-orm/sqlite
5.8.0
jest
28.1.2
The text was updated successfully, but these errors were encountered:
Description
My team and I recently had big performance issues on our app. We managed to fix them after refactoring several collections management.
We usually write entity constructors to always have a consistent model, thus a container (in the following example the sea) is often created with a bunch a elements (fish) passed directly in the constructor. We then persist and flush the container alongside all elements in one call.
It leads to bad performance. Creating and persisting/flushing an empty container which will then be filled with elements is 10 times faster.
To Reproduce
Steps to reproduce the behavior:
jest flush.spec.ts
Expected behavior
I would expect similar performance in both case.
Additional context
Following old issue guided us towards the workaround: #2379
Is this a regression?
Versions
The text was updated successfully, but these errors were encountered: