1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Chapter reflow performance test: random text</title> 5 <script src="../resources/runner.js"></script> 6 </head> 7 <body> 8 <pre id="log"></pre> 9 <div id="target" style="width: 300px; display: none;"> 10 11 </div> 12 <script> 13 var RandomTextGenerator = function() { 14 this.letters = [ 15 String.fromCharCode(RandomTextGenerator.firstCharCode), 16 String.fromCharCode(RandomTextGenerator.firstCharCode), 17 String.fromCharCode(RandomTextGenerator.firstCharCode), 18 String.fromCharCode(RandomTextGenerator.firstCharCode), 19 String.fromCharCode(RandomTextGenerator.firstCharCode), 20 String.fromCharCode(RandomTextGenerator.firstCharCode), 21 String.fromCharCode(RandomTextGenerator.firstCharCode), 22 String.fromCharCode(RandomTextGenerator.firstCharCode), 23 String.fromCharCode(RandomTextGenerator.firstCharCode), 24 String.fromCharCode(RandomTextGenerator.firstCharCode) 25 ] 26 } 27 28 RandomTextGenerator.firstCharCode = 65; // 'A' 29 30 RandomTextGenerator.lastCharCode = 123; // 'z' 31 32 RandomTextGenerator.prototype.advance = function(index) { 33 var charCode = this.letters[index].charCodeAt(0); 34 var newCharCode = charCode + 1; 35 if (newCharCode > RandomTextGenerator.lastCharCode) 36 newCharCode = RandomTextGenerator.firstCharCode; 37 this.letters[index] = String.fromCharCode(newCharCode); 38 return charCode; 39 } 40 41 RandomTextGenerator.prototype.generate = function() { 42 var result = this.letters.join(""); 43 44 var index = 0; 45 while (1) { 46 var charCode = this.advance(index); 47 if (charCode != RandomTextGenerator.lastCharCode) 48 break; 49 ++index; 50 } 51 52 return result; 53 } 54 55 var target = document.getElementById("target"); 56 var style = target.style; 57 var randomTextGenerator = new RandomTextGenerator; 58 59 function test() { 60 var target = document.getElementById("target"); 61 var style = target.style; 62 63 var innerHTML = "<p>"; 64 for (var i = 0; i < 5000; ++i) 65 innerHTML += randomTextGenerator.generate() + " "; 66 innerHTML += "</p>"; 67 target.innerHTML = innerHTML; 68 69 style.display = "block"; 70 style.width = "280px"; 71 target.offsetLeft; 72 style.display = "none"; 73 } 74 75 PerfTestRunner.measureRunsPerSecond({ run: test }); 76 </script> 77 </body> 78 </html> 79