To install the project on your machine, you can either clone the repository using git, or click the download zip button on the right and unzip the file.
To view theoptimized webpage, double-click on index.html or go to (https://anujpanchal57.github.io/Website-Optimization/).
I've optimized
index.htmlpage to achieve at least a 90 PageSpeed score. I've optimized the JavaScript inpizza.htmlto achieve a frame rate of 60fps when we scroll. I've also reduced the amount of time to resize pizzas inpizza.htmlto as much as less than 5 ms.
In this part, I've performed the required operations in index.html in order to optimize the PageSpeed Insights Score to atleast 90.
- Reduced the size of image
pizzaria.jpgto 100px width - Inline the CSS in
css/style.cssinindex.html - Make the google-analytics script async
- Added a media query for
print.css - Use the Web Font Loader to load the Google WebFont
- Minify CSS
- Compress jpg and png files
I've optimized the speed of fps in this section
- Moved all the constants outside the for loop in
updatePositions - Moved the
Math.sincalculation outside the for loop inupdatePositions - Moved
var items = document.getElementsByClassName('mover');to anonymous function to the bottom of the file to stopupdatePositionsfrom re-defining items on each and every scroll event - Used
window.items[i].style.left = items[i].basicLeft + 100 * phase + 'px';instead of using onlyitems - Set the number of pizzas equal to 36 in
document.addEventListener('DOMContentLoaded', function()) - Replaced
querySelectorbygetElementById in document.getElementById("movingPizzas1").appendChild(elem); - Switched to
document.getElementByIdin place ofdocument.querySelectorin the functiondetermineDx - Declared the
var phaseoutside the for loop to prevent it from being created for each and every iteration - Declared the
var elemoutside the loop to prevent it from being created for every iteration - Declared the
movingPizzasoutside the for loop to prevent a DOM call on each and every iteration - Changed CSS for
.mover: Add transform: translateZ(0);towill-change: transform; transform translate3d(0,0,0);and alsobackface-visibility: hidden; - Used
transforminstead ofwindow.items[i].style.left = items[i].basicLeft + 100 * phase + 'px'; - Replaced the
animationReadyto check with the updatePositions in this event listener:window.addEventListener('scroll', updatePositions); - Calculated number of pizzas based on the respective window size
- https://github.com/marc1981/optimization/tree/master/views/js
- https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
- https://github.com/uncleoptimus/udacityP4/blob/gh-pages/views/js/main.js
I've optimized the pizza.html and main.js to achieve the minimal time required.
- Modified the
changePizzaSizesin the following ways: - Moved the
document.document.querySelectorAll(".randomPizzaContainer");outside for loop - Refactored inorder to use a switch to set pizza width to avoid Forced Synchronous Layout and avoid use of
determineDxto determine the new pizza widths - Switched to
document.getElementsByClassNamefromdocument.querySelectorAllin the line ofchangePizzaSizes - Shifted
randomPizzas.lengthto a local variable so, that the array's length property should not be accessed on every iteration - Shifted
var pizzasDiv = document.getElementById("randomPizzas");outside the for loop so, the loop only makes a single DOM call - Changed the CSS for
.randomPizzaContainer: Add transform: translateZ(0);andwill-change: transform;