Skip to content

Commit

Permalink
perf: resolveObjectKey (#8434)
Browse files Browse the repository at this point in the history
* perf: resolveObjectKey
* Fix tests
* prevent string construction
  • Loading branch information
kurkle authored Feb 15, 2021
1 parent 19a91be commit 616a877
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/helpers/helpers.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,26 @@ export function _deprecated(scope, value, previous, current) {
}
}

const emptyString = '';
const dot = '.';
function indexOfDotOrLength(key, start) {
const idx = key.indexOf(dot, start);
return idx === -1 ? key.length : idx;
}

export function resolveObjectKey(obj, key) {
// Special cases for `x` and `y` keys. It's quite a lot faster to aceess this way.
// Those are the default keys Chart.js is resolving, so it makes sense to be fast.
if (key === 'x') {
return obj.x;
}
if (key === 'y') {
return obj.y;
if (key === emptyString) {
return obj;
}
const keys = key.split('.');
for (let i = 0, n = keys.length; i < n && obj; ++i) {
const k = keys[i];
if (!k) {
let pos = 0;
let idx = indexOfDotOrLength(key, pos);
while (idx > pos) {
obj = obj[key.substr(pos, idx - pos)];
if (!obj) {
break;
}
obj = obj[k];
pos = idx + 1;
idx = indexOfDotOrLength(key, pos);
}
return obj;
}
Expand Down

0 comments on commit 616a877

Please sign in to comment.