This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
/
benchmark.js
132 lines (108 loc) · 2.71 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const Benchmark = require('benchmark')
const m = require('.')
const suite = new Benchmark.Suite()
// add tests
suite.add('Get', function () {
const f1 = { foo: { bar: 1 } }
m.get(f1)
f1[''] = 'foo'
m.get(f1, '')
m.get(f1, 'foo')
m.get({ foo: 1 }, 'foo')
m.get({ foo: null }, 'foo')
m.get({ foo: undefined }, 'foo')
m.get({ foo: { bar: true } }, 'foo.bar')
m.get({ foo: { bar: { baz: true } } }, 'foo.bar.baz')
m.get({ foo: { bar: { baz: null } } }, 'foo.bar.baz')
m.get({ foo: { bar: 'a' } }, 'foo.fake')
m.get({ foo: { bar: 'a' } }, 'foo.fake.fake2')
const f2 = {}
Object.defineProperty(f2, 'foo', {
value: 'bar',
enumerable: false
})
m.get(f2, 'foo')
m.get({}, 'hasOwnProperty')
function fn () {}
fn.foo = { bar: 1 }
m.get(fn)
m.get(fn, 'foo')
m.get(fn, 'foo.bar')
const f3 = { foo: null }
m.get(f3, 'foo.bar')
m.get(null, 'foo.bar', false)
m.get('foo', 'foo.bar', false)
m.get([], 'foo.bar', false)
m.get(undefined, 'foo.bar', false)
})
.add('Set', function () {
const func = () => 'test'
let f1 = {}
m.set(f1, 'foo', 2)
f1 = { foo: { bar: 1 } }
m.set(f1, 'foo.bar', 2)
m.set(f1, 'foo.bar.baz', 3)
m.set(f1, 'foo.bar', 'test')
m.set(f1, 'foo.bar', null)
m.set(f1, 'foo.bar', false)
m.set(f1, 'foo.bar', undefined)
m.set(f1, 'foo.fake.fake2', 'fake')
m.set(f1, 'foo.function', func)
function fn () {}
m.set(fn, 'foo.bar', 1)
f1.fn = fn
m.set(f1, 'fn.bar.baz', 2)
const f2 = { foo: null }
m.set(f2, 'foo.bar', 2)
const f3 = {}
m.set(f3, '', 3)
})
.add('Delete', function () {
const func = () => 'test'
func.foo = 'bar'
const inner = {
a: 'a',
b: 'b',
c: 'c',
func
}
const f1 = {
foo: {
bar: {
baz: inner
}
},
top: {
dog: 'sindre'
}
}
m.delete(f1, 'foo.bar.baz.c')
m.delete(f1, 'top')
m.delete(f1, 'foo.bar.baz.func.foo')
m.delete(f1, 'foo.bar.baz.func')
}).add('Has', function () {
const f1 = { foo: { bar: 1 } }
m.has(f1)
m.has(f1, 'foo')
m.has({ foo: 1 }, 'foo')
m.has({ foo: null }, 'foo')
m.has({ foo: undefined }, 'foo')
m.has({ foo: { bar: true } }, 'foo.bar')
m.has({ foo: { bar: { baz: true } } }, 'foo.bar.baz')
m.has({ foo: { bar: { baz: null } } }, 'foo.bar.baz')
m.has({ foo: { bar: 'a' } }, 'foo.fake.fake2')
function fn () {}
fn.foo = { bar: 1 }
m.has(fn)
m.has(fn, 'foo')
m.has(fn, 'foo.bar')
})
// add listeners
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
console.log('Finish')
})
// run async
.run({ async: true })