From 9fd33e74f5437d7a00b1f48f195e7acb05a80a5c Mon Sep 17 00:00:00 2001 From: "erik.corry@gmail.com" Date: Mon, 8 Mar 2010 08:49:14 +0000 Subject: [PATCH] Speed up no-capture case for RegExp.exec(). Review URL: http://codereview.chromium.org/669161 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@4048 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/regexp-delay.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/regexp-delay.js b/src/regexp-delay.js index 6b9ff9c..843d0aa 100644 --- a/src/regexp-delay.js +++ b/src/regexp-delay.js @@ -152,8 +152,12 @@ function RegExpExec(string) { } string = regExpInput; } - var s = ToString(string); - var length = s.length; + var s; + if (IS_STRING(string)) { + s = string; + } else { + s = ToString(string); + } var lastIndex = this.lastIndex; var i = this.global ? TO_INTEGER(lastIndex) : 0; @@ -172,16 +176,23 @@ function RegExpExec(string) { } var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1; - var result = new $Array(numResults); - for (var i = 0; i < numResults; i++) { - var matchStart = lastMatchInfo[CAPTURE(i << 1)]; - var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; - if (matchStart != -1 && matchEnd != -1) { - result[i] = SubString(s, matchStart, matchEnd); - } else { - // Make sure the element is present. Avoid reading the undefined - // property from the global object since this may change. - result[i] = void 0; + var result; + if (numResults === 1) { + var matchStart = lastMatchInfo[CAPTURE(0)]; + var matchEnd = lastMatchInfo[CAPTURE(1)]; + result = [SubString(s, matchStart, matchEnd)]; + } else { + result = new $Array(numResults); + for (var i = 0; i < numResults; i++) { + var matchStart = lastMatchInfo[CAPTURE(i << 1)]; + var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)]; + if (matchStart != -1 && matchEnd != -1) { + result[i] = SubString(s, matchStart, matchEnd); + } else { + // Make sure the element is present. Avoid reading the undefined + // property from the global object since this may change. + result[i] = void 0; + } } } -- 2.7.4