lieko-metrics 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +109 -0
  2. package/lieko-metrics.js +135 -0
  3. package/package.json +17 -0
package/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # LiekoMetrics
2
+
3
+ **Super simple performance measurement helper**
4
+ Works in **Node.js**, **browsers**, and via **CDN** — zero dependencies.
5
+
6
+ Measure code execution time with nice console output and human-readable durations (µs, ms, s, min).
7
+
8
+ ## Quick Start
9
+
10
+ ### 1. Node.js / Bun / Deno (CommonJS or ESM)
11
+
12
+ ```js
13
+ // CommonJS
14
+ const perf = require('lieko-metrics');
15
+
16
+ // or ESM
17
+ import perf from 'lieko-metrics';
18
+
19
+ perf.start('my-task');
20
+ // ... your code ...
21
+ perf.stop('my-task');
22
+ // → ⏱️ my-task perf: 12.4ms
23
+ ```
24
+
25
+ ### 2. Browser – via CDN (recommended)
26
+
27
+ ```html
28
+ <!-- Latest version from jsDelivr -->
29
+ <script src="https://cdn.jsdelivr.net/npm/lieko-metrics@latest/dist/lieko-metrics.min.js"></script>
30
+
31
+ <!-- Or specific version -->
32
+ <!-- <script src="https://cdn.jsdelivr.net/npm/lieko-metrics@1.0.0/dist/lieko-metrics.min.js"></script> -->
33
+ ```
34
+
35
+ ```html
36
+ <script>
37
+ // Global instance ready to use
38
+ liekoPerf.start('page-load');
39
+ // ... your code ...
40
+ liekoPerf.stop('page-load');
41
+
42
+ // Or create your own instance
43
+ const perf = new LiekoMetrics();
44
+ perf.start('heavy-computation');
45
+ // ...
46
+ perf.stop('heavy-computation');
47
+ perf.printSummary();
48
+ </script>
49
+ ```
50
+
51
+ ## Most useful methods
52
+
53
+ ```js
54
+ perf.start('name') // begin measuring
55
+ perf.stop('name') // end + log + save
56
+ perf.printSummary() // beautiful overview of all measurements
57
+
58
+ perf.getStats('name') // → { count, total, average, min, max, latest }
59
+ perf.clear() // reset everything
60
+ ```
61
+
62
+ ## Real-world examples
63
+
64
+ ```js
65
+ // Single shot
66
+ perf.start('render-users');
67
+ renderComplexList(users);
68
+ perf.stop('render-users');
69
+
70
+ // Multiple runs → great for averages
71
+ for (let i = 0; i < 10; i++) {
72
+ perf.start('api:fetch-users');
73
+ await fetch('/api/users');
74
+ perf.stop('api:fetch-users');
75
+ }
76
+
77
+ perf.printSummary();
78
+ // Example output:
79
+ // api:fetch-users:
80
+ // Runs: 10
81
+ // Avg: 342ms
82
+ // Min: 281ms
83
+ // Max: 419ms
84
+ // Last: 355ms
85
+ ```
86
+
87
+ ## Recommended naming style (hierarchical)
88
+
89
+ ```js
90
+ perf.start('page-home:total')
91
+ perf.start('page-home:database:users')
92
+ perf.start('page-home:render:recommendations')
93
+ perf.start('page-home:image:lazy-load')
94
+
95
+ // Stop in reverse order is a good practice
96
+ ```
97
+
98
+ ## Features
99
+
100
+ - Zero dependencies
101
+ - Works in Node.js **and** modern browsers
102
+ - Human-readable durations (`34µs` → `1m 5.2s`)
103
+ - Automatic console logging with emoji
104
+ - Multi-run statistics (avg/min/max/latest)
105
+ - Global `liekoPerf` instance in browser + class `LiekoMetrics`
106
+
107
+ Small, fast, console-friendly — perfect for quick debugging and finding slow parts of your code.
108
+
109
+ Happy profiling!
@@ -0,0 +1,135 @@
1
+ /**
2
+ * LiekoMetrics - Simple performance metrics tool
3
+ * Works in Node.js (CommonJS) and browser (global or CDN)
4
+ * Zero dependencies
5
+ */
6
+
7
+ (function(root, factory) {
8
+ if (typeof define === 'function' && define.amd) {
9
+ define([], factory);
10
+ } else if (typeof module === 'object' && module.exports) {
11
+ module.exports = factory();
12
+ } else {
13
+ const exports = factory();
14
+ root.LiekoMetrics = exports.LiekoMetrics;
15
+ root.liekoPerf = exports.liekoPerf;
16
+ }
17
+ }(typeof self !== 'undefined' ? self : this, function() {
18
+
19
+ /**
20
+ * Convert duration in ms to human-readable format
21
+ * @param {number} ms - Duration in milliseconds
22
+ * @param {number} [precision=2] - Decimal places for µs/ms/s
23
+ * @returns {string} Formatted duration
24
+ */
25
+ function humanDuration(ms, precision = 2) {
26
+ if (typeof ms !== 'number' || isNaN(ms) || ms < 0) {
27
+ return "—";
28
+ }
29
+
30
+ if (ms < 1) {
31
+ return `${(ms * 1000).toFixed(precision)}µs`;
32
+ }
33
+
34
+ if (ms < 1000) {
35
+ return `${ms.toFixed(precision)}ms`;
36
+ }
37
+
38
+ if (ms < 60000) {
39
+ return `${(ms / 1000).toFixed(precision)}s`;
40
+ }
41
+
42
+ const minutes = Math.floor(ms / 60000);
43
+ const seconds = ((ms % 60000) / 1000).toFixed(precision);
44
+
45
+ if (minutes === 0) {
46
+ return `${seconds}s`;
47
+ }
48
+
49
+ return `${minutes}m ${seconds}s`;
50
+ }
51
+
52
+ class LiekoMetrics {
53
+ constructor() {
54
+ this.markers = new Map();
55
+ this.metrics = new Map();
56
+ }
57
+
58
+ start(markerName) {
59
+ if (this.markers.has(markerName)) {
60
+ console.warn(`Marker "${markerName}" already started. Overwriting previous start time.`);
61
+ }
62
+ this.markers.set(markerName, performance.now());
63
+ }
64
+
65
+ stop(markerName) {
66
+ const endTime = performance.now();
67
+ const startTime = this.markers.get(markerName);
68
+
69
+ if (startTime === undefined) {
70
+ console.error(`Marker "${markerName}" was not started.`);
71
+ return null;
72
+ }
73
+
74
+ const duration = endTime - startTime;
75
+
76
+ if (!this.metrics.has(markerName)) {
77
+ this.metrics.set(markerName, []);
78
+ }
79
+ this.metrics.get(markerName).push(duration);
80
+
81
+ this.markers.delete(markerName);
82
+
83
+ console.log(`⏱️ ${markerName} perf: ${humanDuration(duration)}`);
84
+
85
+ return duration;
86
+ }
87
+
88
+ getStats(markerName) {
89
+ const durations = this.metrics.get(markerName) || [];
90
+ if (durations.length === 0) return null;
91
+
92
+ const stats = {
93
+ count: durations.length,
94
+ total: durations.reduce((sum, d) => sum + d, 0),
95
+ average: durations.reduce((sum, d) => sum + d, 0) / durations.length,
96
+ min: Math.min(...durations),
97
+ max: Math.max(...durations),
98
+ latest: durations[durations.length - 1]
99
+ };
100
+
101
+ return stats;
102
+ }
103
+
104
+ printSummary() {
105
+ console.log('\n📊 Performance Summary:');
106
+ console.log('='.repeat(50));
107
+
108
+ for (const [name, durations] of this.metrics) {
109
+ const stats = this.getStats(name);
110
+ if (!stats) continue;
111
+
112
+ console.log(`${name}:`);
113
+ console.log(` Runs: ${stats.count}`);
114
+ console.log(` Avg: ${humanDuration(stats.average)}`);
115
+ console.log(` Min: ${humanDuration(stats.min)}`);
116
+ console.log(` Max: ${humanDuration(stats.max)}`);
117
+ console.log(` Last: ${humanDuration(stats.latest)}`);
118
+ }
119
+ }
120
+
121
+ clear() {
122
+ this.markers.clear();
123
+ this.metrics.clear();
124
+ console.log('🧹 Performance metrics cleared');
125
+ }
126
+ }
127
+
128
+ const liekoPerf = new LiekoMetrics();
129
+
130
+ return {
131
+ LiekoMetrics,
132
+ liekoPerf
133
+ };
134
+
135
+ }));
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "lieko-metrics",
3
+ "version": "0.0.1",
4
+ "description": "Super simple performance metrics helper • start/stop markers • nice console logs • avg/min/max stats • human durations (µs/ms/s/m) • zero dependencies",
5
+ "main": "lieko-metrics.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "type": "commonjs",
13
+ "files": [
14
+ "lieko-metrics.js",
15
+ "README.md"
16
+ ]
17
+ }