Architecture Philosophy
Tokenizador is built with a professional modular architecture that prioritizes maintainability, testability, and separation of concerns. The application follows object-oriented design principles with clear boundaries between different layers of functionality.The architecture is designed to be simple yet scalable, using vanilla JavaScript with ES6 classes to avoid framework overhead while maintaining clean code organization.
System Architecture
The application follows a layered architecture pattern with four distinct layers:Core Design Principles
Separation of Concerns
Each component has a single, well-defined responsibility. The UI layer never directly handles tokenization logic, and services never manipulate the DOM.
Dependency Injection
Components receive their dependencies through constructor injection, making the code more testable and flexible.
Event-Driven Communication
The UI layer communicates with the application layer through event handlers, creating loose coupling between components.
Configuration-Driven
Model data, encodings, and pricing are centralized in configuration files, making updates easy without code changes.
Project Structure
The codebase is organized into logical directories that reflect the architectural layers:Why this structure?
Why this structure?
This structure makes it easy to:
- Locate code - Each file has a clear purpose and location
- Test components - Business logic is isolated from UI concerns
- Add features - New capabilities can be added without modifying existing code
- Maintain consistency - Related code lives together
Component Interaction Flow
Here’s how the components work together during a typical tokenization operation:Data Flow Architecture
Tokenizador uses unidirectional data flow for predictable state management:Unidirectional flow means data always flows in one direction: from user input through processing layers to display output. This makes the application behavior predictable and easier to debug.
Initialization Sequence
When the application starts, components are initialized in a specific order:Error Handling Strategy
The architecture includes multiple levels of error handling:- Service Level - TokenizationService has fallback tokenization when tiktoken fails
- Application Level - TokenAnalyzer catches errors and displays user-friendly messages
- UI Level - UIController validates input and handles DOM errors gracefully
State Management
Tokenizador uses a simple state management approach:- No global state - Each component manages its own internal state
- UI as source of truth - Form inputs (text, model selection) are the source of truth
- Computed values - Statistics and tokens are computed on-demand, not stored
Extensibility Points
The architecture makes it easy to extend functionality:- Add New Model
- Add New Statistic
- Add New Visualization
- Support New Encoding
Simply add an entry to
MODELS_DATA in models-config.js. No code changes needed in services or UI.Performance Considerations
The architecture includes several performance optimizations:DOM Caching
UIController caches DOM element references in
initializeElements() to avoid repeated queriesAsync Operations
Tokenization runs asynchronously to avoid blocking the UI thread
Single Responsibility
Each component does one thing well, avoiding complex computations that could slow down the app
Direct Token Processing
Tokens are processed in a single pass without intermediate storage
Technology Stack
Tokenizador intentionally uses a minimal technology stack:| Layer | Technology | Why? |
|---|---|---|
| Core | Vanilla JavaScript (ES6+) | No build step, no framework overhead |
| Tokenization | tiktoken (WASM) | Industry-standard, same as OpenAI uses |
| Architecture | ES6 Classes | Clean OOP design without frameworks |
| UI | Native DOM APIs | Fast, no virtual DOM overhead |
| Styling | CSS Custom Properties | Themeable without preprocessors |
Next Steps
Component Details
Explore each component in detail with code examples
API Reference
View the complete API documentation
How to Use
Learn how to use Tokenizador
Contributing
Contribute to the project