-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf_test.html
101 lines (86 loc) · 2.37 KB
/
perf_test.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html>
<head>
<title>fasterForEach test</title>
</head>
<body>
<script type="text/javascript">
if (!console.time) {
var startTime, endTime;
console.time = function(str) {
startTime = (new Date()).getTime();
};
console.timeEnd = function(str) {
console.log(str, ' ', (new Date()).getTime() - startTime);
};
}
var items = [];
for (var i = 0; i < 1000000; i++) {
items.push(i);
}
console.time('native');
items.forEach(function(item, idx, array) {
var bla = item * 2;
// console.log(bla);
});
console.timeEnd('native');
</script>
<script type="text/javascript" src="fasterForEach.js"></script>
<script type="text/javascript">
console.time('fasterForEach');
items.fasterForEach(function(item, idx, array) {
var bla = item * 2;
// console.log(bla);
});
console.timeEnd('fasterForEach');
</script>
<script type="text/javascript">
console.time('fasterForEachEval');
items.fasterForEachEval(function(item,idx, array) {
var bla = item * 2;
// console.log(bla);
});
console.timeEnd('fasterForEachEval');
</script>
<script type="text/javascript">
var toObject = function (o) {
if (o == null) { // this matches both null and undefined
throw new TypeError("can't convert "+o+" to object");
}
return Object(o);
};
var boxedString = Object("a");
var splitString = boxedString[0] != "a" || !(0 in boxedString);
var call = Function.prototype.call;
var prototypeOfObject = Object.prototype;
// var _toString = call.bind(prototypeOfObject.toString);
Array.prototype.forEach = function forEach(fun /*, thisp*/) {
var object = toObject(this),
self = splitString && this.toString() == "[object String]" ?
this.split("") :
object,
thisp = arguments[1],
i = -1,
length = self.length >>> 0;
// If no callback function or if callback is not a callable function
if (fun.toString() != "[object Function]") {
// throw new TypeError(); // TODO message
}
while (++i < length) {
if (i in self) {
// Invoke the callback function with call, passing arguments:
// context, property value, property key, thisArg object
// context
fun.call(thisp, self[i], i, object);
}
}
};
console.time('ES5');
items.forEach(function(item, idx, array) {
var bla = item * 2;
// console.log(bla);
});
console.timeEnd('ES5');
</script>
</body>
</html>