Skip to content

perf(js): getValueFromKeysInArray optimization - #29798

Merged
carlosmiei merged 6 commits into
ccxt:masterfrom
ttodua:getvaluefromkey-optim
Aug 24, 2026
Merged

perf(js): getValueFromKeysInArray optimization#29798
carlosmiei merged 6 commits into
ccxt:masterfrom
ttodua:getvaluefromkey-optim

Conversation

@ttodua

@ttodua ttodua commented Aug 12, 2026

Copy link
Copy Markdown
Member
Scenario OLD NEW Speedup
single hit (1 key) 36 ns 12 ns 2.97x
hit after 1 miss 67 ns 22 ns 3.01x
hit after 3 misses 121 ns 42 ns 2.90x
no hit, 3 keys 104 ns 35 ns 2.98x
no hit, 8 keys 255 ns 86 ns 2.97x
empty keys array 21 ns 5.6 ns 3.78x
object is null 7 ns 4.5 ns 1.56x
20 misses + hit 253 ns 214 ns 1.18x

@carlotestor carlotestor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Rewrites getValueFromKeysInArray from Array.find + prop to a straight for…of loop in ts/src/base/functions/type.ts (JS/TS safe*N hot path only — Py/PHP keep their own helpers). Draft, empty body, +13/−1.

Correctness

I diffed old vs new on the usual edges (null/'' skip, 0/false keep, missing keys, nullish key entries, arrays as objects). Behavior matches prop's filter for the cases safe*N actually hit.

One intentional divergence (good): when no listed key hits, old code did object[array.find(…)] with find → undefined, which in JS is object['undefined'] and could return a real value if that key existed. The new loop returns undefined cleanly. Worth a one-liner in the PR body so reviewers don't treat it as accidental drift.

No new test.safeMethods cases; existing N-variant coverage still exercises this helper. A tiny case for the undefined-key footgun would lock the fix in if you want belt-and-suspenders before unmarkedraft.

Notes

  • Drop the blank line inside the arrow body (neighbors prop/prop2 stay dense).
  • k == null works; matching prop's k === undefined || k === null would keep the file consistent.
  • No numbers/bench in the description — a one-line microbench (or even “hot path under safeStringN”) would make the perf(js) claim reviewable.

Labels: enhancement, javascript

Verdict: COMMENT (draft). Looks like a sound micro-opt + small correctness tidy; not blocking on style nits.

Automated triage by carlotestor (Hermes ccxt-new-pr-issue-webhook).

Comment thread ts/src/base/functions/type.ts Outdated
Comment thread ts/src/base/functions/type.ts
@ttodua
ttodua marked this pull request as ready for review August 12, 2026 19:24
Copilot AI lite review requested due to automatic review settings August 12, 2026 19:24
@ttodua

ttodua commented Aug 12, 2026

Copy link
Copy Markdown
Member Author

@kroitor @carlosmiei ready

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes getValueFromKeysInArray in the base type helpers to reduce overhead when selecting the first defined value from a list of candidate keys. This is a core utility used by safe* N helpers and therefore affects behavior/performance across the codebase (and transpiled targets).

Changes:

  • Replaces the prior Array.find(...) + prop(...) approach with a manual loop to reduce allocations/callback overhead.
  • Adds explicit early returns for non-object inputs and skips null/undefined keys during iteration.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread ts/src/base/functions/type.ts
@ttodua ttodua closed this Aug 24, 2026
@ttodua ttodua reopened this Aug 24, 2026
@carlosmiei
carlosmiei requested a review from carlotestor August 24, 2026 16:08
@carlosmiei
carlosmiei merged commit eb15508 into ccxt:master Aug 24, 2026
16 of 28 checks passed
@carlosmiei
carlosmiei deleted the getvaluefromkey-optim branch August 24, 2026 16:09
@carlotestor

Copy link
Copy Markdown
Collaborator

Merge digest

getValueFromKeysInArray drops the Array.find + prop double lookup for a single typed loop, ~3x faster across every key-count scenario.

File Δ What
ts/src/base/functions/type.ts +13/−1 getValueFromKeysInArray
flowchart LR
  type["type"]
Loading

ts/src/base/functions/type.ts

@@ -32,7 +32,19 @@
-const getValueFromKeysInArray = (object: Dictionary<any>, array: any[]) => isObject (object) ? object[array.find ((k: NullableIndexType) => prop (object, k) !== undefined)] : undefined;
+const getValueFromKeysInArray = <T>(
+    object: Dictionary<any>,
+    keys: any[],
+): T | undefined => {
+    if (!isObject (object)) return undefined;
+    for (const k of keys) {
+        if (k === undefined || k === null) continue;
+        const v = object[k];
+        if (v !== undefined && v !== null && (v as unknown) !== '') return v;
+    }
+    return undefined;
+};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants