loopbackio / loopbackio/loopback-next
Performance issues with ModelUtils._coerce and coerceArray
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 5.1k
- Forks
- 1.1k
- Ø Merge
- 2 T. 21 Std.
- Gemergte PRs (30 T.)
- 27
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne in lib/model-utils.js bei etwa den Zeilen 498–503, wo ModelUtils._coerce im try/catch-Pfad coerceArray aufruft. Überprüfe das Verhalten von coerceArray und die im Issue beschriebene create-heavy-Arbeitslast. Stelle anschließend sicher, dass die Koerzierung das bestehende Verhalten ohne die wiederholten Kosten von Exceptions beibehält, und bestätige die Verbesserung durch Profiling.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript
- Bereich
- backend, performance
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100