perf: use inline style instead of getComputedStyle (#6550)

This commit is contained in:
neverland 2020-06-16 22:59:07 +08:00 committed by GitHub
parent 35a4b95ef2
commit fd388d0475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,13 +10,24 @@ export function addUnit(value?: string | number): string | undefined {
return isNumeric(value) ? `${value}px` : value;
}
// cache
let rootFontSize: number;
function getRootFontSize() {
if (!rootFontSize) {
const doc = document.documentElement;
const fontSize =
doc.style.fontSize || window.getComputedStyle(doc).fontSize;
rootFontSize = parseFloat(fontSize);
}
return rootFontSize;
}
function convertRem(value: string) {
const rootStyle = window.getComputedStyle(document.documentElement);
const rootFontSize = parseFloat(rootStyle.fontSize);
value = value.replace(/rem/g, '');
return +value * rootFontSize;
return +value * getRootFontSize();
}
export function unitToPx(value: string | number): number {