-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiling.js
More file actions
57 lines (51 loc) · 1.54 KB
/
profiling.js
File metadata and controls
57 lines (51 loc) · 1.54 KB
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
import { createGetter, createSetter } from "./path-pointer.js";
import lodash from 'lodash'; // npm i lodash
const o = { next: [1], nextL: [1] };
let setter = createSetter(o, 'next[1].value');
let getter = createGetter(o, 'next[1]');
const res = setter(1);
function profile (name, fn, count = 1e4) {
console.time(name);
for(let i = 0; i < count; i++)
fn();
console.timeEnd(name);
}
// const start = Date.now();
const chain = (() => {
const queue = [];
setTimeout(() => {
queue.reduce((acc, fn, i) => {
return acc.then(() => {
fn();
return new Promise(r => setTimeout(r, 300));
})
}, Promise.resolve());
});
return function pushToChain (fn) {
queue.push(fn);
}
})();
// chain(() => Date.now() - start);
// chain(() => Date.now() - start);
// chain(() => Date.now() - start);
// chain(() => Date.now() - start);
chain(() => {
console.log('=====');
profile('READY setter', () => setter(Math.random()), 1e3);
profile('lodash.set', () => lodash.set(o, 'nextL[1]', Math.random()), 1e3);
})
chain(() => {
console.log('=====');
profile('READY getter', () => getter(), 1e3);
profile('lodash.get', () => lodash.get(o, 'nextL[1]'), 1e3);
})
chain(() => {
console.log('=====');
profile('cold:( setter', () => createSetter(o, 'next[1]')(Math.random()), 100);
profile('lodash.set', () => lodash.set(o, 'nextL[1]', Math.random()), 100);
})
chain(() => {
console.log('=====');
profile('cold:( getter', () => createGetter(o, 'next[1]')(), 100);
profile('lodash.get', () => lodash.get(o, 'nextL[1]'), 100);
})