Home php教程 PHP开发 jsp implements the back method in ie

jsp implements the back method in ie

Dec 29, 2016 pm 04:15 PM

Usually we need to optimize the function. The general approach is to get the time at the beginning, and get the time again at the end. The time spent can be found by subtracting the two times. The function runs so fast, basically in milliseconds.

The functions given below are prepared for this purpose.

// 时间转为时间戳(毫秒)
function time2stamp(){
  var d = new Date();
  return Date.parse(d)+d.getMilliseconds();
}
Copy after login

Usage:

var t1 = time2stamp();
// 比较各游览器的DOM运行速度。
var divs = document.getElementByTagName("div"); 
var t2 = time2stamp();
alert("耗时:" + (t2 - t1) + " 毫秒");
Copy after login

New method:

var time1 = new Date
// 比较各游览器的DOM运行速度。
var divs = document.getElementByTagName("div"); 
alert("耗时:" + (new Date - time1) + " 毫秒");
Copy after login

Test the efficiency of various loops, for...in such a slow loop will not be tested!

<!doctype html>
<html dir="ltr">
 <head>
  <meta charset="utf-8"/>
  <title>类</title>
  <script type="text/javascript" charset="utf-8">
   window.onload = function(){
  
    for (var i=0, elm; i<4000; i++) {
     elm = document.createElement("div");
     elm.innerHTML = "第一重div<div>第二重div<div>第三重div</div></div>";
     document.body.appendChild(elm);
    }
    document.getElementById("start-test").onclick = startTest;
    var uuid = 0;
    function startTest() {
     var divs = document.getElementsByTagName("div");
  
     var time1 = new Date();
     var i = -1, n = divs.length;while (++i < n) { 
       divs[i].uuid = uuid++;
     }
     document.getElementById("time-1").innerHTML = new Date() - time1;
  
     var time2 = new Date();
     var i = divs.length; while (--i >= 0) { 
      divs[i].uuid = uuid++;
     }
  
     document.getElementById("time-2").innerHTML = new Date() - time2;
  
     var time3 = new Date();
     for (var i=0, n=divs.length; i<n; i++) {
       divs[i].uuid = uuid++;
     }
     document.getElementById("time-3").innerHTML = new Date() - time3;
  
  
     var time4 = new Date();
     for (var n = divs.length - 1; n >= 0; n--) {
        divs[n].uuid = uuid++;
      }
     document.getElementById("time-4").innerHTML = new Date() - time4;
  
     var time5 = new Date();
     for (var n = divs.length - 1; n--;) {
        divs[n].uuid = uuid++;
      }
     document.getElementById("time-5").innerHTML = new Date() - time5;
  
     var time6 = new Date();
     var n = divs.length-1; do { 
       divs[n].uuid = uuid++;
     } while (n--);
     document.getElementById("time-6").innerHTML = new Date() - time6;
 
     var time7 = new Date();
     for(var i=0,el;el = divs[i++];){
      el.uuid = uuid++;
     };
     document.getElementById("time-7").innerHTML = new Date() - time7;
  
    }
   }
  </script>
 </head>
 <body>
  <style>
   h2 {
    margin:0;
    padding:10px;
    font-weight:500;
    font-size:13px;
    background:#8080C0;
    color:#fff;
   }
   p {
    margin:0;
    text-indent:2em;
    border-bottom:2px solid #a9ea00;
    background:#F2F1D7;
   }
   span.red{
    color:red;
   }
  </style>
  <h1>JavaScript loop speed test</h1>
  <ul>
   <li>遍历页面上12000个DIV</li>
  </ul>
  <input id="start-test" type="button" value="Start test">
  <h2>循环1: var i = -1, n = divs.length;while (++i < n) { }<span>顺序</span></h2>
  <p id="time-1"></p>
  
  <h2>循环2:var n = divs.length; while (--n >= 0) { }<span>倒序</span></h2>
  <p id="time-2"></p>
  
  <h2>循环3:for (var i=0, n=divs.length; i<n; i++)<span>顺序</span></h2>
  <p id="time-3"></p>
  
  <h2>循环4:for (var n = divs.length - 1; n >= 0; n--) { }<span>倒序</span></h2>
  <p id="time-4"></p>
  
  <h2>循环5:for (var n = divs.length - 1; n--;) { }<span>倒序</span></h2>
  <p id="time-5"></p>
  
  <h2>循环6: var n = divs.length-1; do { } while (n--)<span>倒序</span></h2>
  <p id="time-6"></p>
 
  <h2>循环7: for(var i=0,el;el = divs[i++];){ } <span>顺序</span></h2>
  <p id="time-7"></p>
 </body>
</html>
Copy after login

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

For more articles related to JSP implementation of the back method in IE, please pay attention to the PHP Chinese website!


Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24