阿里云主机折上折
  • 微信号
Current Site:Index > Performance optimization recommendations

Performance optimization recommendations

Author:Chuan Chen 阅读数:29384人阅读 分类: HTML

Performance optimization is key to improving webpage loading speed and user experience. Reasonable HTML coding standards can effectively reduce page rendering time, lower resource consumption, and maintain code maintainability.

Reduce DOM Node Count

Excessive DOM nodes increase the computational burden of page rendering. Optimize with the following methods:

  1. Avoid unnecessary nesting levels
<!-- Not recommended -->
<div class="wrapper">
  <div class="container">
    <div class="content">
      <p>Text content</p>
    </div>
  </div>
</div>

<!-- Recommended -->
<div class="content">
  <p>Text content</p>
</div>
  1. Use semantic tags instead of generic divs
<!-- Not recommended -->
<div class="header"></div>
<div class="nav"></div>

<!-- Recommended -->
<header></header>
<nav></nav>

Proper Use of CSS and JavaScript

  1. Place CSS in the document head
<head>
  <link rel="stylesheet" href="styles.css">
</head>
  1. Place JavaScript at the bottom of the document
<body>
  <!-- Page content -->
  <script src="app.js"></script>
</body>
  1. For scripts that don’t affect above-the-fold rendering, add defer or async attributes
<script src="analytics.js" defer></script>
<script src="social.js" async></script>

Optimize Images and Multimedia Resources

  1. Use appropriate image formats
<!-- Use JPEG for photos -->
<img src="photo.jpg" alt="Photo">

<!-- Use SVG for icons and simple graphics -->
<img src="icon.svg" alt="Icon">

<!-- Use PNG for transparency -->
<img src="logo.png" alt="Logo">
  1. Add width and height attributes to avoid layout shifts
<img src="banner.jpg" alt="Banner" width="1200" height="630">
  1. Use the picture element for responsive loading
<picture>
  <source media="(min-width: 800px)" srcset="large.jpg">
  <source media="(min-width: 400px)" srcset="medium.jpg">
  <img src="small.jpg" alt="Responsive image">
</picture>

Reduce Repaints and Reflows

  1. Use transform and opacity for animations
<style>
  .box {
    transition: transform 0.3s ease;
  }
  .box:hover {
    transform: scale(1.1);
  }
</style>
  1. Avoid frequent DOM manipulation
// Not recommended
for (let i = 0; i < 100; i++) {
  document.body.innerHTML += `<div>${i}</div>`;
}

// Recommended
let html = '';
for (let i = 0; i < 100; i++) {
  html += `<div>${i}</div>`;
}
document.body.innerHTML = html;

Use Preload and Preconnect

  1. Preload critical resources
<link rel="preload" href="critical.css" as="style">
<link rel="preload" href="main.js" as="script">
  1. Preconnect to important third-party domains
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://cdn.example.com">

Optimize Form Elements

  1. Add appropriate type attributes to input fields
<input type="email" name="email">
<input type="tel" name="phone">
<input type="number" name="quantity">
  1. Use label to associate with form controls
<label for="username">Username</label>
<input type="text" id="username" name="username">
  1. Add autocomplete to improve user experience
<input type="text" name="address" autocomplete="street-address">

Cache Strategy Optimization

  1. Use manifest files to cache static resources
<html manifest="app.manifest">
  1. Add Cache-Control headers
<meta http-equiv="Cache-Control" content="max-age=31536000">

Reduce HTTP Requests

  1. Inline critical CSS
<style>
  /* Critical CSS content */
  .header { color: #333; }
  .hero { background: #f5f5f5; }
</style>
  1. Combine small icons into sprites
<style>
  .icon {
    background-image: url("sprite.png");
    background-size: 200px 100px;
  }
  .icon-home {
    background-position: 0 0;
    width: 50px;
    height: 50px;
  }
</style>
<div class="icon icon-home"></div>

Use Web Components

  1. Create reusable custom elements
<script>
  class MyComponent extends HTMLElement {
    constructor() {
      super();
      this.attachShadow({ mode: 'open' });
      this.shadowRoot.innerHTML = `
        <style>
          :host {
            display: block;
          }
        </style>
        <div class="component-content">...</div>
      `;
    }
  }
  customElements.define('my-component', MyComponent);
</script>

<my-component></my-component>

Accessibility Optimization

  1. Add appropriate ARIA attributes
<button aria-expanded="false" aria-controls="dropdown">Menu</button>
<div id="dropdown" hidden>Dropdown content</div>
  1. Ensure keyboard accessibility
<a href="#content" class="skip-link">Skip navigation</a>
  1. Add empty alt for decorative images
<img src="divider.png" alt="">

Mobile Optimization

  1. Set viewport meta tag
<meta name="viewport" content="width=device-width, initial-scale=1">
  1. Use touch-friendly sizes
<button style="min-width: 48px; min-height: 48px">Click</button>
  1. Avoid 300ms click delay
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

Server-Side Rendering Optimization

  1. Use SSR to reduce above-the-fold loading time
<!-- Server-rendered HTML -->
<div id="app">
  <!-- Initial rendered content -->
</div>
<script src="client.js"></script>
  1. Stream HTML
// Node.js example
res.write('<!DOCTYPE html><html><head>');
res.write('<title>Page title</title>');
res.write('</head><body>');
// Continue writing content...

Resource Loading Priority

  1. Mark critical resources
<link rel="preload" href="hero-image.jpg" as="image" importance="high">
  1. Defer non-critical resources
<img src="lazy-image.jpg" loading="lazy" alt="Lazy-loaded image">

Reduce Third-Party Script Impact

  1. Use iframes to isolate third-party code
<iframe src="https://example.com/widget" 
        sandbox="allow-scripts allow-same-origin"
        loading="lazy"></iframe>
  1. Load non-essential scripts dynamically
window.addEventListener('load', () => {
  const script = document.createElement('script');
  script.src = 'non-critical.js';
  document.body.appendChild(script);
});

Code Minification and Simplification

  1. Remove HTML comments
<!-- Remove such comments in production -->
  1. Minify whitespace
<div class="compact"><span>Content</span></div>
  1. Use short attribute names
<input type="checkbox" checked>

Modern Browser Feature Detection

  1. Use supports for conditional loading
<link rel="stylesheet" href="modern.css" media="(display-mode: standalone)">
  1. Progressive enhancement strategy
<video controls>
  <source src="video.webm" type="video/webm">
  <source src="video.mp4" type="video/mp4">
  <p>Your browser does not support HTML5 video</p>
</video>

Performance Monitoring and Testing

  1. Add Performance API markers
performance.mark('start-loading');
window.addEventListener('load', () => {
  performance.mark('end-loading');
  performance.measure('page-load', 'start-loading', 'end-loading');
});
  1. Use Lighthouse audits
<!-- Ensure the page includes necessary meta info -->
<meta name="description" content="Page description">

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

上一篇:表单元素规范

下一篇:可访问性要求

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.