-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.js
72 lines (56 loc) · 1.43 KB
/
benchmark.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
const bench = require('nanobench')
const mandatory = ['user:edit']
const scopes = 'user:edit document:write document:read list:manage user:archive'
bench('validate("some space separate scopes")', function (b) {
const scope = require('./')(mandatory)
b.start()
for (let i = 0; i < 10e7; i++) {
scope(scopes)
}
b.end()
})
bench('validate(["some", "scopes", "as", "array"])', function (b) {
const scope = require('./')(mandatory)
const arr = new Set(scopes.split(' '))
b.start()
for (let i = 0; i < 10e7; i++) {
scope(arr)
}
b.end()
})
bench('validate(new Set(["some", "scopes", "as", "array"]))', function (b) {
const scope = require('./')(mandatory)
const arr = scopes.split(' ')
b.start()
for (let i = 0; i < 10e7; i++) {
scope(arr)
}
b.end()
})
bench('traditional string split & array indexOf', function (b) {
const scope = mandatory
let arr
b.start()
for (let i = 0; i < 10e7; i++) {
arr = scopes.split(' ')
for (let j = 0; j < scope.length; j++) {
if (arr.indexOf(scope[j]) === -1) continue
}
}
b.end()
})
bench('traditional for loop & array indexOf', function (b) {
const scope = mandatory
const arr = scopes.split(' ')
b.start()
for (let i = 0; i < 10e7; i++) {
contains(scope, arr)
}
b.end()
})
function contains (mandatory, scopes) {
for (let i = 0; i < mandatory.length; i++) {
if (!~scopes.indexOf(mandatory[i])) return false
}
return true
}