Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
perf(core): prioritize compiling elements in view
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed May 16, 2021
1 parent 35daa99 commit 88f5a65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/core/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ export const flattenElementChildren = (
return collection;
};

const isElementInViewport = (el: HTMLElement): boolean => {
const rect = el.getBoundingClientRect();
const windowHeight = window.innerHeight || document.documentElement.clientHeight;
const windowWidth = window.innerWidth || document.documentElement.clientWidth;

// https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap
const verticalInView = rect.top <= windowHeight && rect.top + rect.height >= 0;
const horizontalInView = rect.left <= windowWidth && rect.left + rect.width >= 0;

return verticalInView && horizontalInView;
};

export const compile = (
el: HTMLElement,
state: State = {},
Expand All @@ -165,7 +177,9 @@ export const compile = (
}
if (hasDirectiveRE().test(element.outerHTML)) {
const newASTNode = createASTNode(element, state);
if (newASTNode) ast.push(newASTNode);
if (newASTNode) {
ast[isElementInViewport(element) ? 'unshift' : 'push'](newASTNode);
}
}
});

Expand Down
1 change: 1 addition & 0 deletions src/core/directives/bind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const bindDirective = ({ el, parts, data, state }: DirectiveProps): void
} else {
/* istanbul ignore next */
el.className = '';
el.removeAttribute('class');
}
}
break;
Expand Down

0 comments on commit 88f5a65

Please sign in to comment.