Skip to content

Commit

Permalink
[fix] Fix some performance bottlenecks (#59)
Browse files Browse the repository at this point in the history
* [fix] Fix some bottlenecks
* [refactor] Simplified maxScroll calculation method
  • Loading branch information
Whotakesmyname authored and Stanko committed Jan 20, 2020
1 parent a058c17 commit 6a13007
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions source/Plx.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const FILTER_PROPERTIES = [
];

// Props to be removed from passing directly to the component element
const PROPS_TO_OMIT = [
const PROPS_TO_OMIT = new Set([
'animateWhenNotInViewport',
'children',
'className',
Expand All @@ -169,7 +169,7 @@ const PROPS_TO_OMIT = [
'tagName',
'onPlxStart',
'onPlxEnd',
];
]);

// Get element's top offset
function getElementTop(el) {
Expand Down Expand Up @@ -384,7 +384,7 @@ function parallax(scrollPosition, start, duration, startValue, endValue, easing)
value += min;
}

return parseFloat(value.toFixed(3));
return Math.floor(value * 1000) / 1000;
}

// Calculates current value for color parallax
Expand Down Expand Up @@ -501,7 +501,7 @@ function omit(object, keysToOmit) {
const result = {};

Object.keys(object).forEach(key => {
if (keysToOmit.indexOf(key) === -1) {
if (!keysToOmit.has(key)) {
result[key] = object[key];
}
});
Expand All @@ -520,7 +520,6 @@ function getNewState(scrollPosition, props, state, element) {
} = props;
const {
showElement,
plxStyle,
plxStateClasses,
} = state;

Expand Down Expand Up @@ -559,13 +558,8 @@ function getNewState(scrollPosition, props, state, element) {
const segments = [];
let isInSegment = false;
let lastSegmentScrolledBy = null;
const maxScroll = Math.max(
document.body.scrollHeight,
document.body.offsetHeight,
document.documentElement.clientHeight,
document.documentElement.scrollHeight,
document.documentElement.offsetHeight
) - window.innerHeight;
const bodyHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
const maxScroll = bodyHeight - window.innerHeight;

for (let i = 0; i < parallaxData.length; i++) {
const {
Expand Down Expand Up @@ -698,10 +692,7 @@ function getNewState(scrollPosition, props, state, element) {
newStyle.OFilter = newStyle.filter;
newStyle.msFilter = newStyle.filter;

// "Stupid" check if style should be updated
if (JSON.stringify(plxStyle) !== JSON.stringify(newStyle)) {
newState.plxStyle = newStyle;
}
newState.plxStyle = newStyle;

// Adding state class
const newPlxStateClasses = getClasses(lastSegmentScrolledBy, isInSegment, parallaxData);
Expand Down

0 comments on commit 6a13007

Please sign in to comment.