|
| 1 | +// MobX Performance Benchmark |
| 2 | +// |
| 3 | +// Hardware Specifications: |
| 4 | +// - Processor: 13th Gen Intel(R) Core(TM) i7-13650HX (14 cores) |
| 5 | +// - RAM: 16 GB |
| 6 | +// - OS: Windows 10 Home (Version 2009) |
| 7 | +// - Node.js: v18+ |
| 8 | +// |
| 9 | +import { makeAutoObservable } from 'mobx'; |
| 10 | +import { createInitialState, measureTime, TEST_CONFIGS, logEnvironmentInfo, runBenchmark, saveBenchmarkResults } from './benchmark-utils.mjs'; |
| 11 | + |
| 12 | +console.log('🏃♂️ MobX Performance Benchmark'); |
| 13 | +console.log('==============================\n'); |
| 14 | + |
| 15 | +// Log environment information |
| 16 | +logEnvironmentInfo(); |
| 17 | + |
| 18 | +// MobX store class with observable state and actions |
| 19 | +class MobxStore { |
| 20 | + constructor(initialState) { |
| 21 | + Object.assign(this, initialState); |
| 22 | + makeAutoObservable(this); |
| 23 | + } |
| 24 | + |
| 25 | + updateProp(propKey, payload) { |
| 26 | + this[propKey] = { ...this[propKey], ...payload }; |
| 27 | + } |
| 28 | + |
| 29 | + batchUpdate(updates) { |
| 30 | + for (const [key, value] of Object.entries(updates)) { |
| 31 | + this[key] = value; |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Benchmark function for MobX |
| 37 | +function benchmarkMobx(stateSize, iterations) { |
| 38 | + const initialState = createInitialState(stateSize); |
| 39 | + |
| 40 | + // Store Creation |
| 41 | + const createResult = measureTime('Store Creation', () => { |
| 42 | + return new MobxStore({ ...initialState }); |
| 43 | + }); |
| 44 | + |
| 45 | + const store = createResult.result; |
| 46 | + |
| 47 | + // Single Update Performance |
| 48 | + const singleUpdateResult = measureTime('Single Update', () => { |
| 49 | + store.updateProp('prop0', { updated: true }); |
| 50 | + return store; |
| 51 | + }); |
| 52 | + |
| 53 | + // Batch Updates Performance |
| 54 | + const batchUpdateResult = measureTime('Batch Updates', () => { |
| 55 | + const updates = {}; |
| 56 | + for (let i = 0; i < Math.min(iterations, 1000); i++) { |
| 57 | + const propKey = `prop${i % stateSize}`; |
| 58 | + const currentProp = store[propKey]; |
| 59 | + updates[propKey] = { |
| 60 | + ...currentProp, |
| 61 | + counter: (currentProp.counter || 0) + 1 |
| 62 | + }; |
| 63 | + } |
| 64 | + store.batchUpdate(updates); |
| 65 | + return store; |
| 66 | + }); |
| 67 | + |
| 68 | + // Property Access Performance |
| 69 | + const accessResult = measureTime('Property Access', () => { |
| 70 | + let sum = 0; |
| 71 | + for (let i = 0; i < iterations; i++) { |
| 72 | + const propKey = `prop${i % stateSize}`; |
| 73 | + const value = store[propKey]; |
| 74 | + sum += value?.id || 0; |
| 75 | + } |
| 76 | + return sum; |
| 77 | + }); |
| 78 | + |
| 79 | + // Estimate memory usage (MobX doesn't have built-in memory tracking) |
| 80 | + const stateSizeBytes = JSON.stringify( |
| 81 | + Object.fromEntries( |
| 82 | + Object.keys(initialState).map((k) => [k, store[k]]) |
| 83 | + ) |
| 84 | + ).length; |
| 85 | + const estimatedMemoryKB = (stateSizeBytes * 50) / 1024; // Rough estimate for 50 states |
| 86 | + |
| 87 | + return { |
| 88 | + creation: createResult.duration, |
| 89 | + singleUpdate: singleUpdateResult.duration, |
| 90 | + batchUpdate: batchUpdateResult.duration, |
| 91 | + propertyAccess: accessResult.duration, |
| 92 | + memoryKB: estimatedMemoryKB, |
| 93 | + batchIterations: Math.min(iterations, 1000), |
| 94 | + iterations |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +// Run benchmarks |
| 99 | +console.log('Starting MobX benchmarks...\n'); |
| 100 | + |
| 101 | +const results = []; |
| 102 | + |
| 103 | +// Small state, many operations |
| 104 | +results.push(runBenchmark('Small State', TEST_CONFIGS.testSizes.small, TEST_CONFIGS.iterations.large, benchmarkMobx)); |
| 105 | + |
| 106 | +// Medium state, medium operations |
| 107 | +results.push(runBenchmark('Medium State', TEST_CONFIGS.testSizes.medium, TEST_CONFIGS.iterations.medium, benchmarkMobx)); |
| 108 | + |
| 109 | +// Large state, fewer operations |
| 110 | +results.push(runBenchmark('Large State', TEST_CONFIGS.testSizes.large, TEST_CONFIGS.iterations.small, benchmarkMobx)); |
| 111 | + |
| 112 | +// Save results to JSON file |
| 113 | +saveBenchmarkResults('mobx', results); |
| 114 | + |
| 115 | +// Summary |
| 116 | +console.log('\n🎯 MOBX PERFORMANCE SUMMARY'); |
| 117 | +console.log('============================'); |
| 118 | + |
| 119 | +results.forEach((result) => { |
| 120 | + console.log(`\n✅ ${result.testName}:`); |
| 121 | + console.log(` Store Creation: ${result.stats.creation.mean.toFixed(2)}ms`); |
| 122 | + console.log(` Single Update: ${result.stats.singleUpdate.mean.toFixed(2)}ms`); |
| 123 | + console.log(` Avg Update: ${(result.stats.batchUpdate.mean / result.results[0].batchIterations).toFixed(2)}ms`); |
| 124 | + console.log(` Avg Property Access: ${(result.stats.propertyAccess.mean / result.iterations).toFixed(2)}ms`); |
| 125 | + if (result.stats.memoryKB.mean > 0) { |
| 126 | + console.log(` Memory Usage: ${result.stats.memoryKB.mean.toFixed(0)}KB (estimated)`); |
| 127 | + } |
| 128 | +}); |
| 129 | + |
| 130 | +console.log('\n' + '='.repeat(50)); |
| 131 | +console.log('🎉 MOBX BENCHMARK COMPLETE!'); |
| 132 | +console.log('✅ MobX performance data ready for comparison'); |
0 commit comments