Performance Optimization Basics
Performance Impact Factors
Data Characteristics
- • JSON file size
- • Nesting depth levels
- • Array lengths
- • String lengths
- • Data type complexity
Environment Factors
- • Available memory
- • CPU processing power
- • Network bandwidth
- • Parser implementation
- • Runtime environment
Optimization Principles
- • Minimize data size: Reduce unnecessary fields and nesting
- • Optimize parsing: Choose efficient parsers and algorithms
- • Memory efficiency: Manage memory allocation and cleanup
- • Lazy loading: Process data only when needed
- • Caching strategies: Cache frequently accessed data
Performance Metrics
- • Parse time: Time to convert JSON to objects
- • Memory usage: Peak and average memory consumption
- • Throughput: Data processed per unit time
- • Latency: Response time for operations
- • CPU utilization: Processing resource usage
Parsing Performance Optimization
Parser Selection and Comparison
| Parser | Language | Performance | Memory Usage | Features |
|---|---|---|---|---|
JSON.parse() | JavaScript | High | Medium | Native, fast |
ujson | Python | Very High | Low | C-based, ultra-fast |
rapidjson | C++ | Very High | Low | SAX/DOM, streaming |
Jackson | Java | High | Medium | Streaming, data binding |
Parsing Optimization Techniques
✅ Best Practices
// Use native parsers when possible
const data = JSON.parse(jsonString);
// For large files, use streaming
const parser = new JSONStream();
parser.on('data', chunk => {
processChunk(chunk);
});
// Avoid repeated parsing
const cache = new Map();
function parseOnce(json) {
if (!cache.has(json)) {
cache.set(json, JSON.parse(json));
}
return cache.get(json);
}❌ Avoid These Patterns
// Don't parse the same data repeatedly
for (let i = 0; i < 1000; i++) {
const obj = JSON.parse(sameJsonString);
// This is wasteful
}
// Avoid eval() for JSON parsing
const obj = eval('(' + jsonString + ')');
// Security risk and slower
// Don't load entire file for partial data
const huge = JSON.parse(hugeJsonFile);
const small = huge.users[0];
// Memory inefficientMemory Management Strategies
Memory Optimization Techniques
Object Pooling
class ObjectPool {
constructor() {
this.pool = [];
}
get() {
return this.pool.pop() || {};
}
release(obj) {
// Clear object properties
Object.keys(obj).forEach(key => {
delete obj[key];
});
this.pool.push(obj);
}
}
const pool = new ObjectPool();
const obj = pool.get();
// Use object...
pool.release(obj);Lazy Loading
class LazyJSON {
constructor(jsonString) {
this.raw = jsonString;
this.parsed = null;
}
get data() {
if (!this.parsed) {
this.parsed = JSON.parse(this.raw);
this.raw = null; // Free memory
}
return this.parsed;
}
}
const lazy = new LazyJSON(largeJsonString);
// Only parsed when accessed
console.log(lazy.data.users);Memory Management Tips
- • Release references to large objects after use
- • Use WeakMap/WeakSet for temporary references
- • Monitor memory usage with profiling tools
- • Implement garbage collection triggers for long-running processes
- • Consider using typed arrays for numeric data
Large Data Processing
Strategies for Large JSON Files
Chunked Processing
Break large files into smaller, manageable chunks
Streaming
Process data as it arrives without loading everything
Pagination
Request data in pages to limit memory usage
Streaming JSON Parser Example
import { Transform } from 'stream';
class JSONStreamParser extends Transform {
constructor() {
super({ objectMode: true });
this.buffer = '';
this.depth = 0;
this.inString = false;
}
_transform(chunk, encoding, callback) {
this.buffer += chunk.toString();
let start = 0;
for (let i = 0; i < this.buffer.length; i++) {
const char = this.buffer[i];
if (char === '"' && this.buffer[i-1] !== '\\') {
this.inString = !this.inString;
}
if (!this.inString) {
if (char === '{') this.depth++;
if (char === '}') {
this.depth--;
if (this.depth === 0) {
// Found complete object
const obj = this.buffer.slice(start, i + 1);
try {
this.push(JSON.parse(obj));
start = i + 1;
} catch (e) {
// Handle parse error
}
}
}
}
}
this.buffer = this.buffer.slice(start);
callback();
}
}Compression Strategies
Compression Methods Comparison
gzip
70-80% reduction
Good balance
brotli
75-85% reduction
Better compression
lz4
50-60% reduction
Fastest speed
JSON Structure Optimization
❌ Verbose Structure
{
"users": [
{
"firstName": "John",
"lastName": "Doe",
"emailAddress": "john@example.com"
}
]
}✅ Compact Structure
{
"u": [
["John", "Doe", "john@example.com"]
],
"k": ["firstName", "lastName", "email"]
}Performance Monitoring
Performance Measurement Tools
Browser DevTools
// Measure parsing time
console.time('JSON Parse');
const data = JSON.parse(largeJsonString);
console.timeEnd('JSON Parse');
// Memory usage
const beforeMem = performance.memory.usedJSHeapSize;
const data = JSON.parse(jsonString);
const afterMem = performance.memory.usedJSHeapSize;
console.log('Memory used:', afterMem - beforeMem);Node.js Profiling
const { performance } = require('perf_hooks');
const start = performance.now();
const data = JSON.parse(jsonString);
const end = performance.now();
console.log(`Parse time: ${end - start}ms`);
// Memory monitoring
const used = process.memoryUsage();
console.log('Memory usage:', {
rss: Math.round(used.rss / 1024 / 1024),
heapTotal: Math.round(used.heapTotal / 1024 / 1024),
heapUsed: Math.round(used.heapUsed / 1024 / 1024)
});Performance Optimization Quick Reference
Optimization Checklist
Choose appropriate parser for data size
Implement streaming for large datasets
Use compression for data transfer
Monitor memory usage patterns
Implement caching strategies