-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
72 lines (61 loc) · 1.6 KB
/
test.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
var sf = require('./sf.js'),
allow = require('str-allow'),
stoptime = require('stoptime');
var isDuck = sf.is(['quack', 'swim', 'flap']),
filter = allow('1234567890'),
filterFast = sf.allow('1234567890'),
duckDebug = function(what) {
what.split(' ').forEach(function(v, i, a) {
console.log(v, isDuck(v));
});
};
Array.prototype.avg = function() {
var av = 0;
var cnt = 0;
var len = this.length;
for (var i = 0; i < len; i++) {
var e = +this[i];
if (!e && this[i] !== 0 && this[i] !== '0') e--;
if (this[i] == e) {
av += e;
cnt++;
}
}
return av / cnt;
};
/* TESTS */
'pro duck hunter'.split(' ').forEach(function(v, i, a) {
sf.run(v, {
duck: function() {
duckDebug('quack swim flap');
},
hunter: function() {
duckDebug('ready aim fire');
},
default: function() {
console.log(isDuck.toString());
}
});
});
/* BENCHMARKS */
var _benchMark = function(filter, k) {
var bench = stoptime();
for(var i=0; i<k; i++) {
for(var j=0; j<k; j++) {
filter('a'+i+'bc'+j);
}
}
return bench.elapsed();
};
var benchMark = function(k) {
var x = _benchMark(filter, k),
y = _benchMark(filterFast, k),
gte = (x>y) ? '>' : '<';
console.log(x, gte, y);
return ((x/y) - 1) * 100;
};
var bmData = [];
for(var i = 500; i<520; i++) {
bmData.push(benchMark(i));
}
console.log('switch-factory is ' + bmData.avg() + '% faster than str-allow');