Repository navigation
Expand file tree
/
Copy pathperformance.test.ts
More file actions
187 lines (151 loc) · 7.63 KB
/
Copy pathperformance.test.ts
File metadata and controls
187 lines (151 loc) · 7.63 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { describe, it, expect } from 'vitest';
import { parse, getStyle } from './index.js';
describe('performance tests', () => {
it('should parse simple styles quickly', () => {
const styleString = 'color:red; padding:10px; margin:20px';
const iterations = 10000;
const start = performance.now();
for (let i = 0; i < iterations; i++) {
parse(styleString);
}
const end = performance.now();
const timePerOperation = (end - start) / iterations;
console.log(`Parse simple styles: ${timePerOperation.toFixed(4)}ms per operation (${iterations} iterations)`);
expect(timePerOperation).toBeLessThan(1); // Should be under 1ms per operation
});
it('should parse complex styles with conditions quickly', () => {
const styleString = 'display:flex; flex-direction:column; gap:10px; padding:15px; background:white; color:black; dark:background:black; dark:color:white; md:flex-direction:row; md:gap:20px; lg:padding:30px; hover:opacity:0.95';
const iterations = 5000;
const start = performance.now();
for (let i = 0; i < iterations; i++) {
parse(styleString);
}
const end = performance.now();
const timePerOperation = (end - start) / iterations;
console.log(`Parse complex styles: ${timePerOperation.toFixed(4)}ms per operation (${iterations} iterations)`);
expect(timePerOperation).toBeLessThan(2); // Should be under 2ms per operation
});
it('should extract styles quickly', () => {
const styleString = 'display:flex; flex-direction:column; gap:10px; padding:15px; background:white; color:black; dark:background:black; dark:color:white; md:flex-direction:row; md:gap:20px; lg:padding:30px; hover:opacity:0.95';
const parsed = parse(styleString);
const iterations = 50000;
const start = performance.now();
for (let i = 0; i < iterations; i++) {
getStyle(parsed, { theme: 'dark', breakpoint: 'md', states: ['hover'] });
}
const end = performance.now();
const timePerOperation = (end - start) / iterations;
console.log(`Extract styles: ${timePerOperation.toFixed(4)}ms per operation (${iterations} iterations)`);
expect(timePerOperation).toBeLessThan(0.5); // Should be under 0.5ms per operation
});
it('should handle large style strings efficiently', () => {
// Generate a large style string with many rules
const rules = [];
const properties = ['margin', 'padding', 'width', 'height', 'color', 'background'];
const conditions = ['', 'dark:', 'md:', 'lg:', 'hover:', 'dark:md:', 'dark:lg:hover:'];
for (const condition of conditions) {
for (const property of properties) {
rules.push(`${condition}${property}-${Math.random().toString(36).substring(7)}`);
}
}
const styleString = rules.join('; ');
const iterations = 1000;
const start = performance.now();
for (let i = 0; i < iterations; i++) {
const parsed = parse(styleString);
getStyle(parsed, { theme: 'dark', breakpoint: 'lg', states: ['hover'] });
}
const end = performance.now();
const timePerOperation = (end - start) / iterations;
console.log(`Large style string (${rules.length} rules): ${timePerOperation.toFixed(4)}ms per operation (${iterations} iterations)`);
expect(timePerOperation).toBeLessThan(10); // Should be under 10ms per operation
});
it('should demonstrate caching benefits', () => {
const styleString = 'color:red; dark:color:white; md:padding:20px; hover:opacity:0.8';
// Parse once (simulating cached parse)
const parsed = parse(styleString);
// Measure multiple getStyle calls with same parsed object
const iterations = 100000;
const start = performance.now();
for (let i = 0; i < iterations; i++) {
getStyle(parsed, { theme: 'dark' });
getStyle(parsed, { breakpoint: 'md' });
getStyle(parsed, { states: ['hover'] });
}
const end = performance.now();
const timePerOperation = (end - start) / iterations;
console.log(`Cached parse with multiple getStyle calls: ${timePerOperation.toFixed(4)}ms per operation (${iterations} iterations)`);
expect(timePerOperation).toBeLessThan(0.5);
});
it('should measure memory footprint', () => {
const styleString = 'display:flex; flex-direction:column; gap:10px; padding:15px; background:white; color:black; dark:background:black; dark:color:white; md:flex-direction:row; md:gap:20px; lg:padding:30px; hover:opacity:0.95';
// Parse multiple times and store
const parsedObjects = [];
const count = 1000;
for (let i = 0; i < count; i++) {
parsedObjects.push(parse(styleString));
}
// Verify they're all properly structured
expect(parsedObjects).toHaveLength(count);
expect(parsedObjects[0].styles.length).toBeGreaterThan(0);
console.log(`Memory footprint test: ${count} parsed objects created`);
});
it('should benchmark real-world usage pattern', () => {
// Simulate a real application with multiple components
const componentStyles = [
'display:flex; align-items:center; padding:10px; background:white; dark:background:gray',
'font-size:14px; color:black; dark:color:white; md:font-size:16px; lg:font-size:18px',
'margin:0; padding:20px; border:1px solid gray; hover:border:blue; active:border:darkblue',
'width:100%; max-width:800px; margin:0-auto; padding:15px; md:padding:30px',
'display:grid; grid-template-columns:1fr; gap:20px; md:grid-template-columns:1fr 1fr; lg:grid-template-columns:1fr 1fr 1fr',
];
const iterations = 1000;
const start = performance.now();
for (let i = 0; i < iterations; i++) {
// Parse all component styles
const parsed = componentStyles.map(s => parse(s));
// Extract styles for different contexts
parsed.forEach(p => {
getStyle(p); // Base
getStyle(p, { theme: 'dark' }); // Dark theme
getStyle(p, { breakpoint: 'md' }); // Tablet
getStyle(p, { theme: 'dark', breakpoint: 'lg' }); // Dark desktop
getStyle(p, { states: ['hover'] }); // Hover state
});
}
const end = performance.now();
const totalTime = end - start;
const timePerIteration = totalTime / iterations;
console.log(`Real-world usage (${componentStyles.length} components): ${timePerIteration.toFixed(4)}ms per render cycle (${iterations} iterations)`);
console.log(`Total time: ${totalTime.toFixed(2)}ms`);
expect(timePerIteration).toBeLessThan(5); // Full render cycle should be under 5ms
});
it('should efficiently handle deeply nested conditions', () => {
const styleString = 'padding:10px; dark:md:hover:active:focus:padding:50px';
const iterations = 10000;
const start = performance.now();
for (let i = 0; i < iterations; i++) {
const parsed = parse(styleString);
getStyle(parsed, { theme: 'dark', breakpoint: 'md', states: ['hover'] });
}
const end = performance.now();
const timePerOperation = (end - start) / iterations;
console.log(`Deeply nested conditions: ${timePerOperation.toFixed(4)}ms per operation (${iterations} iterations)`);
expect(timePerOperation).toBeLessThan(1);
});
it('should handle empty and invalid input gracefully', () => {
const inputs = ['', ';;;', 'invalid', '-', ':', 'no-dashes-here'];
const iterations = 10000;
const start = performance.now();
for (let i = 0; i < iterations; i++) {
inputs.forEach(input => {
const parsed = parse(input);
getStyle(parsed);
});
}
const end = performance.now();
const timePerOperation = (end - start) / iterations;
console.log(`Invalid input handling: ${timePerOperation.toFixed(4)}ms per operation (${iterations} iterations)`);
expect(timePerOperation).toBeLessThan(1);
});
});