web性能优化的15条实用技巧
我们非常重视原创文章,为尊重知识产权并避免潜在的版权问题,我们在此提供文章的摘要供您初步了解。如果您想要查阅更为详尽的内容,访问作者的公众号页面获取完整文章。
JavaScript Performance in Browsers
Javascript performance in browsers is considered a significant usability issue, exacerbated by the blocking nature of Javascript. Browsers typically process UI and JS script execution in a single thread, causing longer execution time for JS and increased waiting time for browser response.
Improving Loading Performance
- Modern browsers allow parallel downloading of JS files, but JS still blocks other resources. Minimizing the impact on the page is recommended by placing JS files at the bottom of the body tag.
- Reducing the number of external script files can enhance performance due to the overhead associated with HTTP requests.
- Dynamic script loading techniques enable non-blocking downloading and execution of files.
- Non-blocking loading libraries like LABjs facilitate the sequential download of files with control over execution order.
Data Storage and JS Performance
Data storage locations within JS, such as literals, variables, array items, and object members, significantly impact performance. Access to literals and local variables is fastest, while array and object access is slower. Minimizing nested object member access and caching commonly used properties can improve performance.
DOM Programming
Accessing and modifying the DOM impacts browser performance due to the subsequent recalculation of page geometry. Techniques like event delegation can reduce event handler counts and improve response times.
Algorithms and Flow Control
Optimizing loops by minimizing property lookup and using techniques like the Duff device can significantly enhance performance. Function-based iterations are generally slower than loop-based iterations.
Strings and Regular Expressions
Browsers optimize string concatenation by allocating more memory to the left-side string in an expression. Using character sets in regex and trimming whitespace are common practices for handling strings.
Responsive UI
UI thread management and using Web Workers for offloading tasks from the UI thread can lead to a more responsive user interface. Breaking intensive tasks into chunks and using timers can prevent UI lock-up.
AJAX Optimization
There are various methods to request data from a server, with caching strategies being crucial for optimizing AJAX performance. Utilizing server settings, local storage, and techniques like beacons can reduce repeated data requests.
Programming Practices
Avoiding redundant work, using literals, native methods, and proper deployment strategies like HTTP compression and CDN usage are recommended for building high-performance JS applications.
Browser Caching
Leveraging browser caching through Expires headers and cache-control can reduce load times and server requests, thereby improving performance.
Component Compression
Compression can reduce response data size significantly, and it's advisable to compress HTML, scripts, styles, and images to enhance load times and performance.
White Screen Phenomenon
White screens occur when browsers wait for complete stylesheet downloads before rendering pages. Placing stylesheets in the head of the document minimizes the white screen effect.
CSS Expressions and DNS Lookups
Using one-time CSS expressions is advised, although avoiding them is best. Reducing DNS lookups and avoiding redirects are also important considerations for performance.
Note: The above tips are part of the full content shared, intended to help improve your web development performance. Remember to share, like, and bookmark if you find them helpful!
想要了解更多内容?
徐小夕【知乎专栏作家】掘金签约作者,定期分享前端工程化,可视化,企业实战项目知识,深度复盘企业中经常遇到的500+技术问题解决方案。【关注趣谈前端,前端路上不迷茫】