Skip to content

Commit 730798c

Browse files
Add caching for gem version comparison to improve performance
Optimize version validation by caching comparison results to avoid repeated normalization on every rendering request. **Changes:** - Add `versionCache` Map to store version comparison results - Cache is checked before performing normalization (key: gemVersion) - Normalization only runs on cache miss - Production logging is now conditional on cache miss (logs only once) **Performance benefits:** - Eliminates repeated string operations (trim, toLowerCase, regex) on every request - Prevents log spam in production (warning logged only once per unique version) - Minimal memory overhead (few cached version strings) **Cache behavior:** - Cache key: gemVersion string - Cache value: boolean (true if versions match, false if mismatch) - If key exists, comparison result is used directly - Production warning only logs when version is first encountered (justCached flag) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent e04c313 commit 730798c

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

react_on_rails_pro/packages/node-renderer/src/worker/checkProtocolVersionHandler.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import log from '../shared/log';
88

99
const NODE_ENV = process.env.NODE_ENV || 'production';
1010

11+
// Cache to store version comparison results to avoid repeated normalization and logging
12+
// Key: gemVersion string, Value: boolean (true if matches, false if mismatch)
13+
// If key exists, it means we've already processed and logged this version (if needed)
14+
const versionCache = new Map<string, boolean>();
15+
1116
/**
1217
* Normalizes a version string to handle differences between Ruby gem and NPM version formats.
1318
* Converts prerelease versions like "4.0.0.rc.1" to "4.0.0-rc.1" for consistent comparison.
@@ -53,17 +58,31 @@ Update either the renderer or the Rails server`,
5358

5459
// Check gem version
5560
if (gemVersion) {
56-
const normalizedGemVersion = normalizeVersion(gemVersion);
57-
const normalizedPackageVersion = normalizeVersion(packageJson.version);
61+
// Check cache first
62+
let versionsMatch = versionCache.get(gemVersion);
63+
let justCached = false;
64+
65+
// If not in cache, perform comparison and cache the result
66+
if (versionsMatch === undefined) {
67+
const normalizedGemVersion = normalizeVersion(gemVersion);
68+
const normalizedPackageVersion = normalizeVersion(packageJson.version);
69+
versionsMatch = normalizedGemVersion === normalizedPackageVersion;
70+
versionCache.set(gemVersion, versionsMatch);
71+
justCached = true;
72+
}
5873

59-
if (normalizedGemVersion !== normalizedPackageVersion) {
74+
// Handle version mismatch
75+
if (!versionsMatch) {
6076
const isProduction = railsEnv === 'production' || NODE_ENV === 'production';
6177

6278
const mismatchMessage = `React on Rails Pro gem version (${gemVersion}) does not match node renderer version (${packageJson.version}). Using exact matching versions is recommended for best compatibility.`;
6379

6480
if (isProduction) {
6581
// In production, log a warning but allow the request to proceed
66-
log.warn(mismatchMessage);
82+
// Only log once per unique gemVersion (when it was first cached)
83+
if (justCached) {
84+
log.warn(mismatchMessage);
85+
}
6786
} else {
6887
// In development, throw an error to prevent potential issues
6988
return {

0 commit comments

Comments
 (0)