Skip to content

Commit 42b78ef

Browse files
authored
Merge pull request #66 from tabkram/feature/doc
docs: update doc and usage examples for `@memoize`, `@cache` and `@trace` decorators
2 parents b7deb75 + 6887581 commit 42b78ef

File tree

2 files changed

+90
-12
lines changed

2 files changed

+90
-12
lines changed

README.md

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@
1212
[![Documentation](https://img.shields.io/badge/documentation-grey?logo=githubpages&color=blue)](https://tabkram.github.io/execution-engine)
1313
[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/execution-engine)
1414

15-
Execution Engine is a TypeScript library that enables tracing and visualization of code execution workflows in your
16-
project. Gain insights into the dynamic sequence of code execution by
17-
capturing detailed traces in JSON format, easily parseable into graphs.
15+
Execution Engine is a TypeScript library that enables tracing, visualization, and optimization of code execution
16+
workflows. It provides tools to trace execution flow, manage caching, and optimize repeated computations, offering
17+
insights through structured execution traces in JSON format.
1818

1919
## Features ✨
2020

21+
Features are divided into two main parts:
22+
23+
### 1. Execution:
24+
25+
- **Trace:** Capture detailed execution flow for debugging and analysis.
26+
- **Cache:** Prevent redundant function executions by storing results temporarily.
27+
- **Memoize:** Optimize repeated computations within the same execution call.
28+
29+
### 2. Engine:
30+
2131
- **Tracing:** Trace the execution flow of code within your project.
2232
- **Timing:** Capture the timing of each executed function.
2333
- **Visualization:** Generate traces in JSON format for clear and insightful visualization.
@@ -39,7 +49,73 @@ yarn add execution-engine
3949

4050
## Usage 📚
4151

42-
### Example 1: Basic Usage
52+
---
53+
### 1. Execution:
54+
55+
#### Example 1: Memoization with `@memoize`
56+
57+
```typescript
58+
import { memoize } from "execution-engine";
59+
60+
class Calculator {
61+
@memoize() // Store the result of Fibonacci calculations
62+
fibonacci(n: number): number {
63+
if (n <= 1) return n;
64+
return this.fibonacci(n - 1) + this.fibonacci(n - 2);
65+
}
66+
}
67+
68+
const calc = new Calculator();
69+
console.log(calc.fibonacci(10)); // Calculates and stores result
70+
console.log(calc.fibonacci(10)); // Reuses pending result, no recalculation
71+
```
72+
73+
In this example, the `fibonacci` method is decorated with `@memoize`, meaning repeated calls with the same `n` will reuse the stored result instead of recalculating it.
74+
75+
76+
#### Example 2: Caching Results with `@cache`
77+
78+
```typescript
79+
import { cache } from "execution-engine";
80+
81+
class ExampleService {
82+
@cache({ ttl: 5000 }) // Store result for 5 seconds
83+
async fetchData(id: number): Promise<string> {
84+
console.log('Fetching data...');
85+
return `Data for ${id}`;
86+
}
87+
}
88+
89+
const service = new ExampleService();
90+
console.log(await service.fetchData(1)); // Fetches data and stores it
91+
console.log(await service.fetchData(1)); // Reuses stored result (within ttl)
92+
```
93+
94+
The `fetchData` method is decorated with `@cache`, storing the result for 5 seconds. Subsequent calls within that time reuse the stored result.
95+
96+
#### Example 3: Tracing with `@trace`
97+
98+
```typescript
99+
import { trace } from "execution-engine";
100+
101+
class MathOperations {
102+
@trace(console.log) // Trace the execution and log using console.log
103+
add(a: number, b: number): number {
104+
return a + b;
105+
}
106+
}
107+
108+
const mathOps = new MathOperations();
109+
console.log(mathOps.add(2, 3)); // Traces the 'add' method execution and logs it
110+
```
111+
112+
In this example, `@trace`logs the method execution, including input parameters, output, timing, duration, start time, end time, and elapsed time.
113+
114+
115+
---
116+
### 2. Engine:
117+
118+
#### Example 1: Basic Usage
43119

44120
```typescript
45121
import { ExecutionEngine } from "execution-engine";
@@ -61,9 +137,10 @@ You can:
61137

62138
- view the **complete code** in [examples/usage.ts](examples/usage.ts)
63139
- inspect the **trace output** in [examples/usage.json](examples/usage.json).
64-
- visualize the **trace graph** using the json-to-graph online tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage.json)
140+
- visualize the **trace graph** using the json-to-graph online
141+
tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage.json)
65142

66-
### Example 2: Usage with Decorators
143+
#### Example 2: Usage with Decorators
67144

68145
```typescript
69146
import { engine, run } from "execution-engine";
@@ -94,9 +171,10 @@ You can:
94171

95172
- view the **complete code** in [examples/usage2.ts](examples/usage2.ts)
96173
- inspect the **trace output** in [examples/usage2.json](examples/usage2.json)
97-
- visualize the **trace graph** using the json-to-graph online tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage2.json)
174+
- visualize the **trace graph** using the json-to-graph online
175+
tool. [→ See the result ←](https://tabkram.github.io/json-to-graph/?data=https://raw.githubusercontent.com/tabkram/execution-engine/main/examples/usage2.json)
98176

99-
### Understanding the Trace 🧭
177+
#### Understanding the Trace 🧭
100178

101179
The `trace` object is an array containing **nodes** and **edges**. It has the following structure:
102180

@@ -130,7 +208,7 @@ trace = [
130208
];
131209
```
132210

133-
## Examples 📘
211+
#### Examples 📘
134212

135213
For additional usage examples, please explore the __[/examples](examples)__ directory in this repository.
136214

src/timer/executionTimer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class ExecutionTimer {
2121
* Starts the execution timer.
2222
* @param executionId - An optional identifier for the execution timer. Defaults to 'default'.
2323
*/
24-
start(executionId?: string) {
24+
start(executionId?: string): void {
2525
this.timer[executionId ?? 'default'] = {
2626
startTime: performance.now(),
2727
endTime: 0
@@ -32,7 +32,7 @@ export class ExecutionTimer {
3232
* Stops the execution timer.
3333
* @param executionId - An optional identifier for the execution timer. Defaults to 'default'.
3434
*/
35-
stop(executionId?: string) {
35+
stop(executionId?: string): void {
3636
if (this.timer[executionId ?? 'default']?.startTime) {
3737
this.timer[executionId ?? 'default'].endTime = performance.now();
3838
}
@@ -44,7 +44,7 @@ export class ExecutionTimer {
4444
* @param fractionDigits – The number of digits to appear after the decimal point; should be a value between 0 and 100, inclusive.
4545
* @returns The duration of the execution timer in milliseconds.
4646
*/
47-
getDuration(executionId?: string, fractionDigits?: number) {
47+
getDuration(executionId?: string, fractionDigits?: number): number {
4848
const timerId = executionId ?? 'default';
4949
if (this.timer[executionId ?? 'default']?.startTime) {
5050
if (!this.timer[executionId ?? 'default'].endTime) {

0 commit comments

Comments
 (0)