forked from yanhaijing/template.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoinVSconcat.html
84 lines (78 loc) · 1.92 KB
/
joinVSconcat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#wrap div:nth-of-type(2n) {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="wrap"></div>
<script src="../template.js"></script>
<script type="text/html" id="tpl">
<div>
渲染字符串:<%=str.slice(0, 10)%>(<%=str.length%>长度),<%=count%>次,共<%=time%>ms, 平均<%=averTime%>ms
</div>
</script>
<script>
var tpl = template(document.getElementById('tpl').innerHTML);
function repeat(str, count) {
var str2 = '';
for (var i = 0; i < count; i++) {
str2 += str;
}
return str2;
}
var str = 'yanhaijing';
var str1 = repeat(str, 1);
var str2 = repeat(str, 10);
var str3 = repeat(str, 100);
var count = 100000;
run([
function() {joinStr(str1, count);},
function() {concatStr(str1, count);},
function() {joinStr(str2, count);},
function() {concatStr(str2, count);},
function() {joinStr(str3, count);},
function() {concatStr(str3, count);},
]);
function run(arr) {
var fn = arr.shift();
if (!fn) {
return 1;
}
window.setTimeout(function () {
fn();
run(arr);
}, 1000);
}
function joinStr(str, count) {
var arr = [];
var time = (new Date).getTime();
for (var i = 0; i < 100000; i++) {
arr.push(str2);
}
var temp = arr.join('');
var time = (new Date).getTime() - time;
temp = null;
write(str, count, time, time/count);
}
function concatStr(str, count) {
var str2 = '';
var time = (new Date).getTime();
for (var i = 0; i < 100000; i++) {
str2 += str;
}
var time = (new Date).getTime() - time;
str2 = null;
write(str, count, time, time/count);
}
function write(str, count, time, averTime) {
document.getElementById('wrap').innerHTML += tpl({str: str, count: count, time: time, averTime: averTime});
}
</script>
</body>
</html>