Skip to content

Instantly share code, notes, and snippets.

@hideo55
Created December 23, 2011 03:14
Show Gist options
  • Save hideo55/1512988 to your computer and use it in GitHub Desktop.
Save hideo55/1512988 to your computer and use it in GitHub Desktop.
V8 Engine cached target string of pattern match.
var assert = function(expr, msg){
var message = 'Assertion failed';
if(msg) message += ': ' + msg;
if(!expr) throw new Error(message);
};
var matchResultA;
(function(){
var A = RegExp('.');
matchResultA = A.exec('foo')[0];
})();
(function(){
var B = new RegExp('.');
var C = new RegExp('.');
assert( B !== C, 'B and C are diffent object' );
var matchResultB = B.exec()[0];
var matchResultC = C.exec()[0];
assert( matchResultA === matchResultB, 'Pattern match result of B equals result of A' );
assert( matchResultB === matchResultC, 'Pattern match result of C equals result of B' );
assert( matchResultA === /./.exec()[0] );
assert( /.../.exec()[0] == 'foo' );
// String 'foo' cached by V8?
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment