mirror of
https://github.com/chartjs/Chart.js.git
synced 2024-10-07 20:59:08 +02:00
perf: resolveObjectKey (#8434)
* perf: resolveObjectKey * Fix tests * prevent string construction
This commit is contained in:
parent
19a91bebfb
commit
616a877772
@ -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) {
|
export function resolveObjectKey(obj, key) {
|
||||||
// Special cases for `x` and `y` keys. It's quite a lot faster to aceess this way.
|
if (key === emptyString) {
|
||||||
// Those are the default keys Chart.js is resolving, so it makes sense to be fast.
|
return obj;
|
||||||
if (key === 'x') {
|
|
||||||
return obj.x;
|
|
||||||
}
|
}
|
||||||
if (key === 'y') {
|
let pos = 0;
|
||||||
return obj.y;
|
let idx = indexOfDotOrLength(key, pos);
|
||||||
}
|
while (idx > pos) {
|
||||||
const keys = key.split('.');
|
obj = obj[key.substr(pos, idx - pos)];
|
||||||
for (let i = 0, n = keys.length; i < n && obj; ++i) {
|
if (!obj) {
|
||||||
const k = keys[i];
|
|
||||||
if (!k) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
obj = obj[k];
|
pos = idx + 1;
|
||||||
|
idx = indexOfDotOrLength(key, pos);
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user