Size Optimization with @nevware21/ts-utils
This guide provides detailed insights into using @nevware21/ts-utils to optimize your bundle size, with concrete measurements and strategies.
Table of Contents
- NPM Package Sizes
- Bundle Size Impact
- Detailed Size Comparisons
- Tree-Shaking Optimization
- Common Usage Patterns and Their Size Impact
- Advanced Size Optimization Strategies
NPM Package Sizes
Bundle Sizes | Full Unminified | minified | GZipped |
---|---|---|---|
ES5 (Umd) | |||
ES6 (Umd) |
Bundle Size Impact
The @nevware21/ts-utils library is designed with size optimization as a primary goal. By replacing standard JavaScript methods with their ts-utils equivalents, you can significantly reduce your final bundle size through better minification.
Core Size Metrics
Import Method | Minified Size | Gzipped Size |
---|---|---|
Full Library | ||
Tree-shaken (typical usage) | ~5-15 KB | ~2-5 KB |
Individual functions | ~0.1-0.3 KB per function | ~0.05-0.15 KB per function |
While the complete library is already small, the real advantage comes from tree-shaking and importing only what you need.
Detailed Size Comparisons
The following table provides byte-level comparisons of common operations using native JavaScript versus @nevware21/ts-utils:
Operation | Native JS (bytes) | With ts-utils (bytes) | Savings | Notes |
---|---|---|---|---|
Type checking (isArray, isObject, etc.) | ~421 bytes | ~128 bytes | ~70% | Multiple type checks combined |
Array operations (forEach, map, filter) | ~687 bytes | ~289 bytes | ~58% | Basic array iteration operations |
Object property access | ~524 bytes | ~214 bytes | ~59% | Safe property access patterns |
String manipulations | ~836 bytes | ~312 bytes | ~63% | Common string operations |
DOM helpers | ~398 bytes | ~153 bytes | ~62% | Basic DOM checks and operations |
These measurements are for minified code and demonstrate the cumulative effect when these operations are used repeatedly throughout your codebase.
Tree-Shaking Optimization
@nevware21/ts-utils is designed for optimal tree-shaking with modern bundlers:
// Good: Import individual functions
import { isArray, arrForEach } from "@nevware21/ts-utils";
// Bad: Imports everything, prevents effective tree-shaking
import * as tsUtils from "@nevware21/ts-utils";
Module Size Breakdown
Each functional area’s approximate contribution to bundle size when fully imported:
Module | Approximate Size (minified) |
---|---|
Type checking utilities | 2.8 KB |
Array operations | 3.2 KB |
Object utilities | 4.5 KB |
String functions | 3.8 KB |
Symbol polyfills | 1.6 KB |
Math utilities | 1.2 KB |
Timer functions | 2.3 KB |
Environment detection | 1.8 KB |
Common Usage Patterns and Their Size Impact
Example 1: Type Checking and Array Operations
// Native JS approach: ~512 bytes minified
function processItems(items) {
if (Array.isArray(items) && items.length > 0) {
for (let i = 0; i < items.length; i++) {
if (items[i] && typeof items[i] === 'object' && !Array.isArray(items[i])) {
// Process object
}
}
}
}
// ts-utils approach: ~198 bytes minified (61% smaller)
import { isArray, arrForEach, isObject } from "@nevware21/ts-utils";
function processItems(items) {
if (isArray(items) && items.length > 0) {
arrForEach(items, item => {
if (item && isObject(item)) {
// Process object
}
});
}
}
Example 2: Object Property Safety
// Native JS approach: ~389 bytes minified
function getNestedValue(obj) {
if (obj && typeof obj === 'object' &&
obj.data && typeof obj.data === 'object' &&
obj.data.user && typeof obj.data.user === 'object' &&
typeof obj.data.user.name === 'string') {
return obj.data.user.name;
}
return null;
}
// ts-utils approach: ~142 bytes minified (64% smaller)
import { getValueByKey } from "@nevware21/ts-utils";
function getNestedValue(obj) {
return getValueByKey(obj, ["data", "user", "name"]) || null;
}
Advanced Size Optimization Strategies
- Selective Importing: Only import the specific functions you need
// Instead of import { isArray, arrForEach, arrMap, arrFilter } from "@nevware21/ts-utils"; // If you're only using one function frequently, import just that import { arrForEach } from "@nevware21/ts-utils";
- Function Reuse: Reuse imported functions throughout your codebase rather than mixing approaches
// Consistent usage of isArray will maximize minification benefits import { isArray } from "@nevware21/ts-utils"; // Use isArray consistently throughout your code instead of sometimes // using Array.isArray and sometimes using the imported function
- Bundle Analysis: Use tools like Webpack Bundle Analyzer to identify size impact
# Add as a dev dependency npm install --save-dev webpack-bundle-analyzer
- Consider Function Frequency: Focus on replacing frequently used utility patterns
// If your code frequently checks object properties, using objHasOwnProperty // will yield greater size benefits than replacing rarely used operations
Real-World Size Impact
In a typical medium-sized application (50-100KB minified JavaScript):
- Light usage of ts-utils (5-10 functions): ~1.5-3KB savings (~3-6%)
- Moderate usage (10-25 functions): ~3-7KB savings (~6-14%)
- Heavy usage (25+ functions used throughout): ~7-15KB savings (~14-30%)
These savings compound with increased code size and repetition of utility patterns.
Conclusion
While individual function replacements may seem to offer minimal savings (100-300 bytes), the cumulative effect across a modern web application can be substantial. The most significant benefits come from:
- Consistent usage patterns throughout your codebase
- Effective tree-shaking with specific imports
- Focusing on replacing frequently used utility functions
- Leveraging the minification benefits for repeated operations
By strategically incorporating ts-utils into your development workflow, you can achieve meaningful size optimizations while also gaining the benefits of a more consistent, cross-platform API.