#performance
78 snippets tagged with #performance
Redis Cache Get/Set Helper
Type-safe Redis cache wrapper with automatic JSON serialization, TTL support, and cache-aside pattern.
Best for: Database query caching
Node.js Worker Threads for Parallel Processing
Use Worker Threads to run CPU-intensive tasks in parallel without blocking the event loop.
Best for: CPU-intensive data processing without blocking
Node.js Cluster Mode for Scaling
Scale Node.js across CPU cores using the cluster module with automatic worker respawning.
Best for: Utilizing all CPU cores for HTTP servers
Performance Measurement
Measure execution time with performance.now(), Performance API marks, measures, and PerformanceObserver.
Best for: Function execution profiling
HTTP/2 Server with Stream Push
Create an HTTP/2 server with server push, multiplexed streams, and TLS configuration in Node.js.
Best for: High-performance web servers
In-Memory Caching with LRU Strategy
Implement LRU cache with TTL expiration, size limits, and cache-aside pattern for Node.js applications.
Best for: API response caching
ETag Caching Middleware
Express middleware that generates ETags and handles 304 Not Modified responses for bandwidth savings.
Best for: API caching
Response Compression Middleware
Native zlib-based middleware that gzip-compresses responses above a size threshold.
Best for: Reducing API payload size
Multipart Upload Stream Handler
Handles multipart file uploads with streaming to disk without buffering the entire file in memory.
Best for: Large file uploads
In-Memory Response Cache
Simple TTL-based in-memory cache middleware for GET endpoints that serves cached responses.
Best for: Caching expensive queries
JSON Stream Parser
Parses large JSON arrays from a readable stream without loading the entire file into memory.
Best for: Processing large log files
useDebounce — Debounced Value Hook
Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.
Best for: Search-as-you-type
Infinite Scroll with Intersection Observer
Load more items as the user scrolls using IntersectionObserver. No external libraries required.
Best for: Feed pagination
Virtualized List Component
Render large lists efficiently by only rendering visible items with calculated scroll positioning.
Best for: Rendering 10k+ item lists
Image Lazy Load Component
Lazy load images with a blur-up placeholder effect using Intersection Observer and CSS transitions.
Best for: Image galleries
React Virtual List Component
Render large lists efficiently with windowing to only mount visible items in the viewport.
Best for: Rendering thousands of list items efficiently
React Intersection Observer Hook
Custom useIntersectionObserver hook for lazy loading, infinite scroll, and scroll animations.
Best for: Lazy loading components when they enter viewport
useThrottle Hook for Rate Limiting
Throttle rapidly-firing values like scroll or resize events with a configurable delay hook.
Best for: Scroll position tracking
Context Selector Pattern
Avoid unnecessary re-renders with a context selector pattern that subscribes to specific state slices.
Best for: High-performance global state
useScrollPosition Hook
Tracks window scroll position with throttling for performance-sensitive scroll effects.
Best for: Sticky headers
Responsive Image Component
Image component with lazy loading, blur placeholder, error fallback, and srcset support.
Best for: Image galleries
useNetworkSpeed Hook
Monitors the Network Information API to report connection type, downlink speed, and effective type.
Best for: Adaptive media quality
usePrefetch Hook
Prefetches data or resources when the user hovers over an element to reduce perceived latency.
Best for: Route prefetching
useDebouncedCallback Hook
Returns a debounced version of a callback that delays execution until the pause in calls.
Best for: Search input debouncing
useThrottledValue Hook
Throttles a rapidly changing value to update at most once per specified interval.
Best for: Scroll position tracking
Virtual Scroll List
Virtualized list that only renders visible items for large datasets with fixed row height.
Best for: Large data lists
Next.js Image Optimization Patterns
Use next/image with responsive sizes, blur placeholders, and priority loading for optimal Core Web Vitals.
Best for: Hero images
Partial Prerendering with Suspense
Combine static shells with streamed dynamic content using React Suspense for instant page loads.
Best for: E-commerce product pages
Next.js Streaming with Suspense
Stream server components with Suspense boundaries for progressive page loading and better TTFB.
Best for: Progressive loading for data-heavy dashboards
Next.js Image Optimization Patterns
Advanced next/image usage with responsive sizes, blur placeholders, and custom loaders.
Best for: Optimizing Core Web Vitals with proper image loading
Caching Strategies in Next.js
Master Next.js caching with fetch cache, unstable_cache, revalidatePath, and revalidateTag patterns.
Best for: ISR page caching
Dynamic Import and Code Splitting
Use next/dynamic for lazy loading components with custom loading states, SSR control, and named exports.
Best for: Reducing initial bundle size
Parallel Data Fetching in Server Components
Fetch multiple data sources in parallel using Promise.all in Next.js server components for faster page loads.
Best for: dashboard pages
Font Optimization with next/font
Use next/font for automatic self-hosted font optimization with zero layout shift.
Best for: custom typography
Next Image Responsive Patterns
Use next/image with responsive sizes, blur placeholders, and priority loading for optimal image delivery.
Best for: responsive images
Script Component Loading Strategies
Load third-party scripts efficiently with next/script using different loading strategies.
Best for: analytics
Link Prefetching Strategies
Control route prefetching behavior with next/link for optimal navigation performance.
Best for: navigation optimization
Keyset Pagination vs Offset
Efficient keyset (cursor) pagination pattern compared to traditional OFFSET for large datasets in PostgreSQL.
Best for: API list endpoints
Materialized View with Auto-Refresh
Create and maintain materialized views for expensive aggregate queries with concurrent refresh support.
Best for: Dashboard analytics
LATERAL Join for Top-N Per Group
Use LATERAL joins to efficiently fetch the top N related rows per group without window function subqueries.
Best for: Top-N per group queries
Table Partitioning by Range
Partition large tables by date range for faster queries and easier data lifecycle management.
Best for: Time-series data
EXISTS vs IN Subquery Patterns
Choose between EXISTS and IN subqueries for optimal performance based on data distribution.
Best for: Filtering with related tables
Covering Index (INCLUDE Columns)
Create covering indexes with INCLUDE columns to satisfy queries entirely from the index.
Best for: Index-only scans
SQL Index Strategy Patterns
Create effective indexes including composite, partial, covering, and expression-based indexes.
Best for: Optimizing slow database queries
SQL Materialized View Pattern
Create and manage materialized views for caching expensive queries with refresh strategies.
Best for: Caching expensive analytics queries
SQL EXPLAIN ANALYZE for Query Tuning
Use EXPLAIN ANALYZE to understand query plans, identify bottlenecks, and optimize slow queries.
Best for: Diagnosing and fixing slow database queries
Create and Refresh Materialized Views
Use materialized views to cache expensive query results for fast reads.
Best for: Dashboard caching
Read EXPLAIN ANALYZE Output
Use EXPLAIN ANALYZE to understand and optimize query execution plans.
Best for: Query performance tuning
Generator Pipeline for Data Processing
Chain generators to build memory-efficient data processing pipelines for large files and streams.
Best for: Large file ETL
LRU Cache with TTL Support
Extend functools.lru_cache with time-based expiration for caching expensive function calls with staleness control.
Best for: Database query caching
functools.cache and lru_cache
Memoize expensive function calls with functools.cache and lru_cache for automatic result caching.
Best for: Recursive algorithms
Python Itertools Recipes
Practical itertools patterns for batching, flattening, grouping, and combining iterables.
Best for: Efficient batch processing of large datasets
Optimize Memory with __slots__
Reduce memory usage for classes with many instances using __slots__.
Best for: Memory-constrained applications
Batch Process Embeddings Efficiently
Process large datasets of embeddings with batching, caching, and rate limiting.
Best for: Large-scale indexing
Bash Parallel Jobs — Run Tasks Concurrently
Execute multiple shell commands in parallel with job control, exit code checking, and max concurrency.
Best for: Running CI checks in parallel for faster builds
Git Shallow Clone for Large Repos
Shallow clone techniques to reduce download size by limiting commit history depth.
Best for: Speeding up CI/CD pipeline cloning
Git Sparse Checkout — Clone Only What You Need
Use sparse checkout to clone specific directories from large monorepos without downloading everything.
Best for: Working on a specific package in a monorepo
Polars Lazy Query — Fast DataFrame Processing
Use Polars lazy evaluation for high-performance data transformations that outperform pandas.
Best for: High-performance data processing replacing pandas
Polars DataFrame Operations
High-performance DataFrame operations using Polars: filtering, groupby, joins, and lazy evaluation.
Best for: data transformation
Pandas Vectorised Operations vs Apply
Compare apply vs vectorised pandas operations for performance-critical column transformations.
Best for: feature engineering
Multiprocessing Pool for ETL
Parallelise CPU-bound ETL transformations across multiple CPU cores using multiprocessing.Pool.
Best for: parallel file processing
Redis Cache-Aside Pattern in Python
Implement cache-aside (lazy loading) with Redis and Python to accelerate repeated database queries.
Best for: query caching
NumPy Advanced Indexing Patterns
Use fancy indexing, boolean masks, and np.where for fast array transformations without loops.
Best for: numerical computing
Pandas .eval() for Fast Column Computation
Use DataFrame.eval() for expressive, fast in-place column calculations using numexpr.
Best for: large DataFrame operations
PyArrow Dataset Scan with Predicate Pushdown
Scan a partitioned Parquet dataset with column pruning and row-level predicate pushdown via PyArrow.
Best for: lakehouse queries
Fast JSON Serialisation with orjson
Use orjson for 5-10x faster JSON serialisation of large Python dicts, dataclasses, and NumPy arrays.
Best for: high-throughput serialisation
Polars Join Strategies
Perform inner, left, cross, and anti joins in Polars with optimal join strategies.
Best for: data enrichment
Polars Expressions API Patterns
Use Polars expression API for complex column-level transformations without apply or loops.
Best for: column transformations
Pandas read_csv with Explicit Dtypes
Specify column dtypes on CSV read to avoid costly inference and prevent silent type coercion.
Best for: fast CSV loading
Pandas nlargest / nsmallest
Efficiently retrieve the N largest or smallest rows without sorting the full DataFrame.
Best for: top-N queries
Spring Boot Caching with Redis
Add caching to Spring Boot services with @Cacheable, @CacheEvict, TTL configuration, and Redis.
Best for: Reducing database load with application-level caching
Parallel Stream Processing
Use parallel streams for CPU-intensive tasks: when to use, performance tips, and thread-safety.
Best for: CPU-intensive data processing with multi-core utilization
Connection Pool Implementation
Build a generic connection pool with size limits, idle timeout, health checks, and metrics.
Best for: Database connection pooling
Object Pool Pattern
Reuse expensive objects with an object pool: thread-safe checkout, return, health validation, and eviction.
Best for: Reusing expensive-to-create objects
String Pool and Interning
Understand Java string pooling: intern(), identity vs equality, memory optimization, and common pitfalls.
Best for: Memory optimization for repeated string values
Sequences — Lazy Evaluation for Large Data
Process large datasets efficiently with lazy sequences: generateSequence, yield, and custom iterators.
Best for: Processing large datasets without loading all into memory
Parallel Processing with Collections
Process data in parallel using parallel collections, Future.traverse, and batched execution.
Best for: Speeding up CPU-bound operations
Collection Views for Lazy Operations
Use collection views for efficient lazy transformations: avoid intermediate collections and improve performance.
Best for: Memory-efficient data processing