Index: src/string.js |
diff --git a/src/string.js b/src/string.js |
index 49f403de7da8140bb8aeebd9d87c00c329850a0d..4319560324d8cb249444d76d83f1d66c6c13f574 100644 |
--- a/src/string.js |
+++ b/src/string.js |
@@ -162,15 +162,36 @@ function StringLocaleCompare(other) { |
} |
+var stringMatchCacheInput; |
+var stringMatchCacheId; |
+var stringMatchCacheResult; |
+ |
// ECMA-262 section 15.5.4.10 |
function StringMatch(regexp) { |
if (!IS_REGEXP(regexp)) regexp = new ORIGINAL_REGEXP(regexp); |
var subject = TO_STRING_INLINE(this); |
- if (!regexp.global) return regexp.exec(subject); |
+ if (!regexp.global) return RegExpExec.call(regexp, subject); |
%_Log('regexp', 'regexp-match,%0S,%1r', [subject, regexp]); |
+ |
+ var reid = %_RegExpId(regexp); |
+ if (%_IsIdentical(reid, stringMatchCacheId) && |
+ %_IsIdentical(this, stringMatchCacheInput)) { |
+ %_Log("regexp", "regexp-match-cache-hit", []); |
+ var cachedResult = stringMatchCacheResult; |
+ if (cachedResult) { |
+ cachedResult = ArraySlice.call(cachedResult, 0, cachedResult.length); |
+ } |
+ return cachedResult; |
+ } |
+ %_Log("regexp", "regexp-match-cache-miss", []); |
+ |
// lastMatchInfo is defined in regexp-delay.js. |
- return %StringMatch(subject, regexp, lastMatchInfo); |
+ var result = %StringMatch(subject, regexp, lastMatchInfo); |
+ stringMatchCacheInput = this; |
+ stringMatchCacheId = reid; |
+ stringMatchCacheResult = result; |
+ return result; |
} |
@@ -239,13 +260,33 @@ function StringReplace(search, replace) { |
} |
+var stringReplaceRegExpCacheInput; |
+var stringReplaceRegExpCacheId; |
+var stringReplaceRegExpCacheIndex; |
+var stringReplaceRegExpCacheReplace; |
+var stringReplaceRegExpCacheResult; |
// Helper function for regular expressions in String.prototype.replace. |
function StringReplaceRegExp(subject, regexp, replace) { |
+ if (%_IsIdentical(%_RegExpId(regexp), stringReplaceRegExpCacheId) && |
+ %_IsIdentical(subject, stringReplaceRegExpCacheInput) && |
+ %_IsIdentical(replace, stringReplaceRegExpCacheReplace) && |
+ (!regexp.global || |
+ %_IsIdentical(regexp.lastIndex, stringReplaceRegExpCacheIndex))) { |
+ %_Log("regexp", "regexp-replace-cache-hit", []); |
+ return stringReplaceRegExpCacheResult; |
+ } |
+ %_Log("regexp", "regexp-replace-cache-miss", []); |
+ stringReplaceRegExpCacheId = %_RegExpId(regexp); |
+ stringReplaceRegExpCacheInput = subject; |
+ stringReplaceRegExpCacheIndex = regexp.lastIndex; |
+ stringReplaceRegExpCacheReplace = replace; |
replace = TO_STRING_INLINE(replace); |
- return %StringReplaceRegExpWithString(subject, |
- regexp, |
- replace, |
- lastMatchInfo); |
+ var result = %StringReplaceRegExpWithString(subject, |
+ regexp, |
+ replace, |
+ lastMatchInfo); |
+ stringReplaceRegExpCacheResult = result; |
+ return result; |
}; |