Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Side by Side Diff: src/string.js

Issue 1207004: Cache regular expressions used in String.search. (Closed)
Patch Set: Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. 1 // Copyright 2006-2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 var parameters = $Array(m + 2); 514 var parameters = $Array(m + 2);
515 for (var j = 0; j < m; j++) { 515 for (var j = 0; j < m; j++) {
516 parameters[j] = CaptureString(subject, matchInfo, j); 516 parameters[j] = CaptureString(subject, matchInfo, j);
517 } 517 }
518 parameters[j] = index; 518 parameters[j] = index;
519 parameters[j + 1] = subject; 519 parameters[j + 1] = subject;
520 return replace.apply(null, parameters); 520 return replace.apply(null, parameters);
521 } 521 }
522 522
523 523
524 var re_cache = {}
525
526
524 // ECMA-262 section 15.5.4.12 527 // ECMA-262 section 15.5.4.12
525 function StringSearch(re) { 528 function StringSearch(re) {
526 var regexp = new $RegExp(re); 529 var regexp = re_cache[re];
530 if (IS_UNDEFINED(regexp)) {
sandholm 2010/03/25 05:36:24 Maybe reset cache at size 10, 100 or 1000 to avoid
531 regexp = new $RegExp(re);
532 re_cache[re] = regexp;
533 }
527 var s = TO_STRING_INLINE(this); 534 var s = TO_STRING_INLINE(this);
528 var last_idx = regexp.lastIndex; // keep old lastIndex 535 var last_idx = regexp.lastIndex; // keep old lastIndex
Lasse Reichstein 2010/03/25 08:05:13 Use: var match = DoRegExpExec(regexp, s, 0); i
Lasse Reichstein 2010/03/25 08:53:11 I'll just make a changelist for this on its own.
529 regexp.lastIndex = 0; // ignore re.global property 536 regexp.lastIndex = 0; // ignore re.global property
530 var result = regexp.exec(s); 537 var result = regexp.exec(s);
531 regexp.lastIndex = last_idx; // restore lastIndex 538 regexp.lastIndex = last_idx; // restore lastIndex
532 if (result == null) 539 if (result == null)
533 return -1; 540 return -1;
534 else 541 else
535 return result.index; 542 return result.index;
536 } 543 }
537 544
538 545
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 "small", StringSmall, 993 "small", StringSmall,
987 "strike", StringStrike, 994 "strike", StringStrike,
988 "sub", StringSub, 995 "sub", StringSub,
989 "sup", StringSup, 996 "sup", StringSup,
990 "toJSON", StringToJSON 997 "toJSON", StringToJSON
991 )); 998 ));
992 } 999 }
993 1000
994 1001
995 SetupString(); 1002 SetupString();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698