-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathmmkv.ts
63 lines (56 loc) · 1.74 KB
/
mmkv.ts
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
import { Observable, EventData, Page } from '@nativescript/core';
import { DemoSharedMmkv } from '@demo/shared';
import { NSCMMKV } from '@nativescript/mmkv';
export function navigatingTo(args: EventData) {
const page = <Page>args.object;
page.bindingContext = new DemoModel();
}
const key = 'k';
const iterations = 1000;
export class DemoModel extends DemoSharedMmkv {
mmkv: NSCMMKV;
constructor() {
super();
this.mmkv = new NSCMMKV();
this.mmkv.clearAll();
this.mmkv.clearMemoryCache();
// this.mmkv.set('first', 'Osei');
// this.mmkv.set('age', 30);
// this.mmkv.set('metal', false);
// if (global.isAndroid) {
// const js = new java.lang.String('Fortune');
// const buf = java.nio.ByteBuffer.wrap(js.getBytes());
// this.mmkv.set('last', (ArrayBuffer as any).from(buf as any));
// } else {
// const data = NSString.stringWithString('Fortune').dataUsingEncoding(NSUTF8StringEncoding);
// this.mmkv.set('last', interop.bufferFromData(data));
// }
// console.log(this.mmkv.getString('first'));
// console.log(this.mmkv.getNumber('age'));
// console.log(this.mmkv.getBoolean('metal'));
// console.log(this.mmkv.getString('last'));
// console.time('test');
this.mmkv.set(key, 'hello');
this.bm(this.getFromMMKV.bind(this));
//this.mmkv.close();
}
getFromMMKV(): string | undefined {
return this.mmkv.getString(key);
}
bm(func) {
try {
console.log(`Starting Benchmark...`);
const start = performance.now() as any;
for (let i = 0; i < iterations; i++) {
func();
}
const end = performance.now() as any;
const diff = end - start;
console.log(`Finished Benchmark ! Took ${diff.toFixed(4)}ms!`);
return diff;
} catch (e) {
console.error(`Failed Benchmark !`, e);
return 0;
}
}
}