-
Notifications
You must be signed in to change notification settings - Fork 36
/
bench.js
74 lines (64 loc) · 1.5 KB
/
bench.js
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
var j = require('./index')
, n = 1000000
console.log('Benchmarking, %s times', n)
var lap = stopWatch('toGregorian')
toGregorianBench()
lap()
lap = stopWatch('toJalaali')
toJalaaliBench()
lap()
lap = stopWatch('isLeapJalaaliYear')
isLeapJalaaliYearBench()
lap();
lap = stopWatch('isValidJalaaliDate')
isValidJalaaliDateBench()
lap();
function stopWatch(name) {
var niceName = name + Array(20 - name.length).join(' ')
console.time(niceName)
return function () {
console.timeEnd(niceName)
}
}
function toGregorianBench() {
var count = n
, f = j.toGregorian
while (true)
for (var y = 1; y <= 3000; y += 1)
for (var m = 1; m <= 12; m += 1)
for (var d = 1; d <= 30; d += 1) {
f(y, m, d)
if (--count === 0) return
}
}
function toJalaaliBench() {
var count = n
, f = j.toJalaali
while (true)
for (var y = 560; y <= 3560; y += 1)
for (var m = 1; m <= 12; m += 1)
for (var d = 1; d <= 30; d += 1) {
f(y, m, d)
if (--count === 0) return
}
}
function isLeapJalaaliYearBench() {
var count = n
, f = j.isLeapJalaaliYear
while (true)
for (var y = 1; y <= 3000; y += 1) {
f(y)
if (--count === 0) return
}
}
function isValidJalaaliDateBench() {
var count = n
, f = j.isValidJalaaliDate
while (true)
for (var y = 1; y <= 3000; y += 1)
for (var m = 1; m <= 13; m += 1)
for (var d = 1; d <= 32; d += 1) {
f(y, m, d)
if (--count === 0) return
}
}