UNPKG

setu.js

Version:

A lightweight HTTP client for Node.js and the browser, with smart adapter selection.

106 lines (76 loc) • 2.88 kB
# Setu.js C++ Optimization Module This directory contains the C++ native addon that provides optimized implementations of performance-critical functions when `optimized: true` is enabled in Setu.js requests. ## šŸš€ Performance Benefits The C++ implementation provides significant performance improvements for: - **URL parsing and construction** - Faster string operations - **Query string building** - Optimized parameter encoding - **Header merging** - Efficient object operations - **JSON stringification** - Faster serialization - **Byte length calculation** - Optimized UTF-8 handling - **String operations** - Reduced memory allocations ## šŸ“¦ Building The native module is built automatically during `npm install` or can be built manually: ```bash npm run build:native ``` ### Requirements - Node.js >= 16 - Python 3.x (for node-gyp) - C++ compiler (GCC/Clang on Linux/Mac, Visual Studio on Windows) - node-gyp (installed automatically) ### Platform Support - āœ… Linux - āœ… macOS - āœ… Windows ## šŸ”§ Usage Enable optimization by setting `optimized: true` in your request config: ```js import setu from 'setu.js'; // Use C++ optimized functions const response = await setu.get('/api/data', { optimized: true // Enable C++ optimizations }); ``` The library automatically falls back to JavaScript implementations if: - The native module is not available - Building the native module fails - The module fails to load ## šŸ—ļø Architecture ``` native/ ā”œā”€ā”€ binding.gyp # Build configuration ā”œā”€ā”€ src/ │ └── setu_optimized.cc # C++ implementation └── README.md # This file src/ └── optimized.ts # TypeScript wrapper with JS fallbacks ``` ## šŸ” Functions Optimized 1. **buildQuery** - Build query strings from objects 2. **mergeHeaders** - Merge header objects 3. **buildUrl** - Construct URLs from base and path 4. **byteLength** - Calculate UTF-8 byte length 5. **stringifyJson** - JSON stringification 6. **parseJson** - JSON parsing ## āš ļø Notes - The native module is **optional** - Setu.js works perfectly without it - Browser environments always use JavaScript (native modules are Node.js only) - Performance gains are most noticeable with: - High-frequency requests - Large payloads - Complex query parameters - Many concurrent requests ## šŸ› Troubleshooting If the native module fails to build: 1. Ensure you have Python 3.x installed 2. Install build tools: - Linux: `build-essential` package - macOS: Xcode Command Line Tools - Windows: Visual Studio Build Tools 3. The library will automatically fall back to JavaScript ## šŸ“Š Benchmarks Performance improvements vary by use case, but typical gains are: - **Query building**: 2-5x faster - **Header merging**: 1.5-3x faster - **URL construction**: 2-4x faster - **JSON operations**: 1.2-2x faster