loopbackio / loopbackio/loopback-next
Performance issues with ModelUtils._coerce and coerceArray
Nessuno ha ancora preso questa issue.
- Lingua principale
- TypeScript
- Stelle
- 5.1k
- Fork
- 1.1k
- Merge medio
- 2g 21h
- PR unite (30g)
- 27
Descrizione
Describe the bug
The way ModelUtils._coerce uses coerceArray is inefficient, and is causing it to show up as 20% of my application's CPU usage in profiler runs under a create heavy workload.
The problem code is this in juggler:
try {
// Coerce val into an array if it resembles an array-like object
val = coerceArray(val);
} catch (e) {
// NOOP when not coercable into an array.
}
Throwing and catching an error is much more expensive than returning a value, and nearly every call to _coerce hits this try/throw/catch path.

A variant of coerceArray that doesn't throw, but instead returns [arrayVal | undefined, coerced: boolean] I think would fix the performance problem here.
Logs
No response
Additional information
No response
Reproduction
design issue
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in lib/model-utils.js intorno alle righe 498-503, dove ModelUtils._coerce chiama coerceArray nel percorso try/catch. Esamina il comportamento di coerceArray e il carico di lavoro create-heavy descritto nell'issue, quindi verifica che la coercizione mantenga il comportamento esistente senza il costo ripetuto delle eccezioni e conferma il miglioramento tramite profiling.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript
- Ambito
- backend, performance
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100