-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.js
99 lines (71 loc) · 1.9 KB
/
proxy.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
const Benchmark = require('benchmark')
const suite = new Benchmark.Suite
// 產生長度為 <length> 的隨機字串
function getRandomString(length) {
return String( Math.round( Math.random() * (length * 10) ) )
}
function Tree() {
return new Proxy({}, {
get(target, key, receiver) {
if (!(key in target)) {
target[key] = Tree()
}
return Reflect.get(target, key, receiver)
}
})
}
const cache = {}
const cacheLength = 1000 // 假設 cache 有1000筆
const testKey = 'testkey'
function memorizeFunc(...args) {
let key
// let data = Tree()
// eval(`data[${args.join('][')}] = 123`)
// obj = {}
function createObj(arr, changeObj = {}) {
let a = arr.shift()
console.log(changeObj)
// if(!Object.keys(obj).length) {
// // obj[a] = changeObj[a] = {}
// return changeObj[a] = {}
// } else {
// changeObj[a] = {}
// }
if(arr.length) createObj(arr, changeObj[a])
}
createObj([1,2,3])
// if(args.length === 1 ){
// [key] = args
// } else {
// key = JSON.stringify(args)
// }
if( key in cache ) {
return cache[key]
} else {
return cache[key] = getRandomString(1000)
}
}
for (let i = 0; i <= cacheLength; i+=1) {
const random = Math.random()
const randomArray = Array.from({length: random < 0.3 ? 1 : random < 0.6 ? 2 : 3 }).map(x => getRandomString(1000))
memorizeFunc(...randomArray)
if( cacheLength === cacheLength / 2 ) {
// 欲抓取的 key
memorizeFunc(testKey)
memorizeFunc(testKey, testKey)
memorizeFunc(testKey, testKey , testKey)
}
}
suite
.add('1 param', () => {
memorizeFunc(testKey)
})
.add('2 params', () => {
memorizeFunc(testKey, testKey)
})
.add('3 params', () => {
memorizeFunc(testKey, testKey, testKey)
})
.on('cycle', event => console.log(String(event.target)))
.on('complete', () => console.log('complete'))
.run({ 'async': true })