Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 10, 2025

🎯 Summary

Implements in-memory caching for modules to significantly improve performance by avoiding redundant file operations, as requested in issue #27.

📋 Changes Made

  • Module Caching: Added moduleCache Map to store imported modules by their resolved path
  • Built-in Caching: Added builtinCache Map to cache built-in modules by module:environment key
  • baseUse Enhancement: Modified to check cache before importing and store results after processing
  • Built-in Resolver Enhancement: Modified to cache built-in module results to avoid repeated imports
  • Comprehensive Testing: Added tests/cache.test.mjs with 5 test cases covering different caching scenarios
  • Demo Example: Added examples/cache-demo.mjs to demonstrate caching performance improvements

🧪 Test Coverage

All new caching functionality is thoroughly tested:

  • ✅ Module cache returns same instance on repeated calls
  • ✅ Built-in module cache works correctly
  • ✅ Different modules are cached separately
  • ✅ Same object reference returned for cached modules
  • ✅ Built-in cache works with both regular and node: prefixed imports

🔧 Technical Implementation

Module Caching

// In-memory cache for loaded modules to avoid redundant file operations
const moduleCache = new Map();

export const baseUse = async (modulePath) => {
  // Check if module is already cached
  if (moduleCache.has(modulePath)) {
    return moduleCache.get(modulePath);
  }
  // ... import and process module ...
  // Cache the processed module for future use
  moduleCache.set(modulePath, processedModule);
  return processedModule;
};

Built-in Module Caching

// Cache for built-in modules to avoid redundant imports
const builtinCache = new Map();

// Create cache key including environment for built-in modules
const cacheKey = `${moduleName}:${environment}`;
if (builtinCache.has(cacheKey)) {
  return builtinCache.get(cacheKey);
}

🚀 Performance Impact

  • First Import: Normal loading time (file system + processing)
  • Subsequent Imports: Near-instant return of cached module
  • Memory Usage: Minimal increase due to caching references
  • Behavior: 100% backward compatible - no breaking changes

📁 Files Modified

  • use.mjs - Added caching to module and built-in resolvers
  • use.js - Applied same caching changes for browser compatibility
  • use.cjs - Applied same caching changes for CommonJS compatibility
  • tests/cache.test.mjs - New comprehensive test suite
  • examples/cache-demo.mjs - New demo script

Fixes #27

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #27
@konard konard self-assigned this Sep 10, 2025
konard and others added 2 commits September 10, 2025 07:34
- Add moduleCache Map to cache imported modules by their path
- Add builtinCache Map to cache built-in modules by module:environment key
- Modify baseUse function to check cache before importing and store results
- Modify builtin resolver to cache results to avoid repeated imports
- Add comprehensive tests to verify caching behavior works correctly
- Add cache demo example script showing performance improvements

Fixes #27

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] In memory cache of modules (not to read files all the time) Implement in-memory cache for modules Sep 10, 2025
@konard konard marked this pull request as ready for review September 10, 2025 04:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In memory cache of modules (not to read files all the time)

2 participants