search
HomeWeb Front-endH5 TutorialHTML5 best practices to make web apps faster

HTML5 best practices to make web apps faster

Apr 29, 2017 pm 01:23 PM
html5web appBest Practices

Introduction

This article focuses on how to make full use of HTML5 and CSS to make web apps run more smoothly.

Tip 1: Use web storage instead of cookies

The biggest drawback of cookies is that all cookie data that conforms to the rules will be carried in every HTTP request. This will increase the request response time, especially XHR requests. It is better to use sessionStorage and localStorage instead of cookies in HTML5.

This alternative method can store data locally in the user's local area permanently or with session time. The data will not be transferred with the HTTP request. So we give priority to using web storage and only use cookies as an alternative.

// if localStorage is present, use that
if (('localStorage' in window) && window.localStorage !== null) {

  // easy object property API
  localStorage.wishlist = '["unicorn", "Narwhal", "deathbear"]';

} else {

  // without sessionStorage we'll have to use a far-future cookie
  // with document.cookie's awkward API
  var date = new Date();
  date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
  var expires = date.toGMTString();
  var cookiestr = 'wishlist=["unicorn", "Narwhal", "deathbear"];' +
                  ' expires=' + expires + '; path=/';
  document.cookie = cookiestr;
}

Tip 2: Use CSS Transition instead of JavaScript animation

CSS Transition can bring higher performance, less code, and easier maintenance and understanding.

Tip 3: Use client database instead of server request

Web SQL Database and IndexedDB give browsers database storage capabilities. Many application scenarios can be migrated to client databases to reduce the number of server requests.

LocalStorage and sessionStorage are faster than client databases in simple data storage and can be used to implement some simple states and save progress.

When a component needs to manage hundreds of pieces of data (such as a friend list) and support user search, filtering, and sorting, storing a copy of the data in the client database can effectively reduce the number of HTTP requests. See the Web SQL Database tutorial for detailed guidance.

Tip 4: Use JavaScript native API

With the popularity of higher versions of JavaScript, many new APIs have been added, such as Array prototype, which can be used directly in most browsers. For example:

// give me a new array of all values multiplied by 10
[5, 6, 7, 8, 900].map(function (value) {
  return value * 10;
});
// [50, 60, 70, 80, 9000]

// create links to specs and drop them into #links.
var linksList = document.querySelector('#links');
var links = [];
['html5', 'css3', 'webgl'].forEach(function (value) {

  links.push(value.link('http://google.com/search?btnI=1&q=' + value + ' spec'));
});
linksList.innerHTML = links.join('');

// return a new array of all mathematical constants under 2
[3.14, 2.718, 1.618].filter(function (number) {
  return number <p>
Usually these native methods are faster than manually writing loops:</p><pre class="brush:php;toolbar:false">for (var i = 0, len = arr.length; i <p>
Using native JSON.parse() is more efficient and safer than json2.js.</p><p>
The native String.prototype.trim is also a good example. These functions are not in HTML5 and should be widely used.</p><h2>
Tip 5: Not only use cache manifest for offline apps, but also use it appropriately for online websites</h2><p>
Sites such as backend management systems can greatly improve performance by using cache.</p><p>
Cache manifest has some advantages over setting Expires: it clearly declares the files that need to be cached, the browser can optimize them, and they may have been downloaded locally before you use them.</p><p>
The basic structure of the page can be regarded as a template. The displayed content changes with the data. The templateable HTML structure is cached through cache.manifest, and the content is updated after obtaining the JSON data from the server.</p><p>
​See the application cache tutorial for detailed instructions.</p><h2>
Tip 6: Enable hardware acceleration to enhance the visual experience</h2><p>
Some browsers may use GPU acceleration to make high-speed animations smoother. Firefox Minefield, IE9, and Safari have claimed to implement hardware acceleration. Chromium has also added 3D transform acceleration for the window platform. Each browser will definitely support more and more hardware acceleration. The better.</p><p>
When hardware acceleration is supported and enabled, animation, rotation, scaling, and opacity will definitely be smoother. All actual operations occur on the GPU without redrawing the content. However, it should be noted that any operation that affects the page layout It will all reduce the speed.</p><h2>
Tip 7: Use web workers to perform operations that require a lot of CPU resources</h2><p>
Web workers have two benefits: 1) Fast 2) Does not block browser response. Click the web worker slide to view more information.</p><p>
Some possible usage scenarios for web workers:</p>
  • Long text formatting


  • Syntax Highlighting


  • Image processing


  • Picture synthesis


  • Large array processing

Tip 8: HTML5 form attributes and input types

HTML5 adds a series of input types, including search, tel, url, email, datetime, date, month, week, time, number, range, color, etc. Use native functions in browsers that support these functions, and use js plug-ins as a supplement .

Things like placeholder, required, and pattern can greatly improve the usability and performance of the page.

​Click on the HTML5 form information to view more information.

Tip 9: Use CSS3 to reduce the use of images

Reducing images can reduce HTTP requests, reduce page size, and make maintenance easier. Commonly used attributes are as follows:

  • linear and radial gradients


  • border-radius


  • box-shadow


  • rgba


  • transform


  • css mask

Common usage scenarios include: polished buttons via gradients, replicate many other effects

Tip 10: Use WebSocket instead of XHR to provide faster interaction and less bandwidth

WebSockets is designed for Comet. Using it to implement Comet does bring more benefits than XHR.

Original link: http://www.html5rocks.com/en/tutorials/speed/quick/

The above is the detailed content of HTML5 best practices to make web apps faster. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
H5: How It Enhances User Experience on the WebH5: How It Enhances User Experience on the WebApr 19, 2025 am 12:08 AM

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

Deconstructing H5 Code: Tags, Elements, and AttributesDeconstructing H5 Code: Tags, Elements, and AttributesApr 18, 2025 am 12:06 AM

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

Understanding H5 Code: The Fundamentals of HTML5Understanding H5 Code: The Fundamentals of HTML5Apr 17, 2025 am 12:08 AM

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

H5 Code: Best Practices for Web DevelopersH5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AM

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

H5: The Evolution of Web Standards and TechnologiesH5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AM

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web DevelopmentH5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)