Repository navigation
Expand file tree
/
Copy pathperf.ts
More file actions
70 lines (68 loc) · 2.65 KB
/
Copy pathperf.ts
File metadata and controls
70 lines (68 loc) · 2.65 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
58
59
60
61
62
63
64
65
66
67
68
69
70
/* tslint:disable */
import * as reworkCssParse from 'css-parse';
import * as fs from "fs";
import * as shadyCss from 'shady-css-parser';
import * as nativescriptCss from '../src/index';
import { Tokenizer } from '../src/index';
const themeCoreLightIos = fs.readFileSync(`${__dirname}/assets/core.light.css`).toString();
function trapDuration(action: () => void) {
const [startSec, startMSec] = process.hrtime();
action();
const [endSec, endMSec] = process.hrtime();
return (endSec - startSec) * 1000 + (endMSec - startMSec) / 1000000;
}
const charCodeByCharCodeDuration = trapDuration(() => {
let count = 0;
for (let i = 0; i < themeCoreLightIos.length; i++) {
count += themeCoreLightIos.charCodeAt(i);
}
});
const charByCharDuration = trapDuration(() => {
let char;
for (let i = 0; i < themeCoreLightIos.length; i++) {
char = themeCoreLightIos.charAt(i);
}
});
const compareCharIfDuration = trapDuration(() => {
let c = 0;
for (let i = 0; i < themeCoreLightIos.length; i++) {
const char = themeCoreLightIos[i];
if ((char >= "a" && char <= "z") || (char >= "A" && char <= "Z") || char === "_") {
c++;
}
}
});
const compareCharRegEx = /[a-zA-Z_]/;
const compareCharRegExDuration = trapDuration(() => {
let char;
let c = 0;
for (let i = 0; i < themeCoreLightIos.length; i++) {
const char = themeCoreLightIos[i];
if (compareCharRegEx.test(char)) {
c++;
}
}
});
const indexerDuration = trapDuration(() => {
let char;
for (let i = 0; i < themeCoreLightIos.length; i++) {
char = themeCoreLightIos[i];
}
});
const reworkDuration = trapDuration(() => {
const ast = reworkCssParse(themeCoreLightIos, { source: "nativescript-theme-core/css/core.light.css" });
});
const shadyDuration = trapDuration(() => {
const shadyParser = new shadyCss.Parser();
const ast = shadyParser.parse(themeCoreLightIos);
});
const nativescriptParseDuration = trapDuration(() => {
const cssparser = new nativescriptCss.Parser();
// Shaved from 6.2 to 5.5 msecs.
cssparser.debug = false;
// TODO: Replace with parseAStylesheet when ready
const stylesheet = cssparser.parseACSSStylesheet(themeCoreLightIos);
});
console.log(`Baseline perf: .charCodeAt: ${charCodeByCharCodeDuration.toFixed(3)}ms. .charAt: ${charByCharDuration.toFixed(3)}ms. []: ${indexerDuration.toFixed(3)}ms. compareCharIf: ${compareCharIfDuration.toFixed(3)}ms. compareCharRegEx: ${compareCharRegExDuration.toFixed(3)}ms.
Parsers perf: rework: ${reworkDuration.toFixed(3)}ms. shady: ${shadyDuration.toFixed(3)}ms. {N}: ${nativescriptParseDuration.toFixed(3)}ms.
`);