Repository navigation
Replies: 1 comment 1 reply
|
n = 4000? That's a lot of objects! You should only include maybe 100 objects at a time 😂. Anyway, accepted. |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Use a Set to dedupe
includedresources (19× faster at n=4000)Version: 1.12.5 (current
latest)Files:
src/utils/serializer.utils.ts,src/classes/serializer.tsSummary
When building a compound document, ts-japi deduplicates
includedresources using an array withkeys.includes(key). That membership check is a linear scan, and it runs once per candidate resource — so serializing n included resources costs ~n²/2 string comparisons.Switching
keysfromArraytoSetmakes it O(n). The array is only ever used for membership testing and appending, so the change is semantically identical.Where
recurseRelatorsDepth:recurseRelatorshas the same pattern.keysoriginates inSerializer.serializeasconst keys = [].Benchmark
Isolated
npm install ts-japi@1.12.5, two copies differing only by the Set change. One serializer, one relator,includedepth 1, each of n primary resources related to a distinct resource (soincludedgrows to n). Median of 3 runs, warmup discarded,process.hrtime.bigint(), Node v24.4.1.includedGrowth per doubling of n:
Below n≈500 both are dominated by fixed overhead and the difference is noise — at n=100 the two are indistinguishable. From n=1000 the quadratic signature is unambiguous: the array approaches ~5.8× per doubling while the Set holds ~1.9–2.1×, i.e. linear.
One caveat stated plainly: ~5.8× per doubling is steeper than the 4× a pure O(n²) predicts. The extra is most likely allocation/GC and cache-miss effects as the key array grows. The asymptotic direction is not in doubt, but treat the constants as indicative rather than exact.
Correctness: outputs are byte-identical (
JSON.stringifyequality) between the two variants, with matchingincludedcounts at every n.Proposed fix
Two call sites in
serializer.ts, two inserializer.utils.ts. Thekeysparameter type changes fromstring[]toSet<string>— internal only, not part of the public API.Happy to open a PR with the fix and a regression benchmark if that's useful.
All reactions