From 2fc877e8757944121a68c8e2047fc0f6c7ba0483 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 3 Sep 2011 19:42:41 +0200 Subject: [PATCH] Match literals using |RegExp.prototype.test| This is little faster than |String.prototype.match| in successful cases since return value of |test| is just a boolean, not a special array as with |match|. Speed impact ------------ Before: 130.75 kB/s After: 131.81 kB/s Difference: 0.81% Size impact ----------- Before: 1059811 b After: 1058371 b Difference: -0.14% (Measured by /tools/impact with Node.js v0.4.8 on x86_64 GNU/Linux.) --- src/emitter.js | 2 +- src/parser.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/emitter.js b/src/emitter.js index d0e30ab..6154423 100644 --- a/src/emitter.js +++ b/src/emitter.js @@ -747,7 +747,7 @@ PEG.compiler.emitter = function(ast) { } return formatCode( - "if (input.substr(pos).match(${regexp}) !== null) {", + "if (${regexp}.test(input.substr(pos))) {", " ${resultVar} = input.charAt(pos);", " pos++;", "} else {", diff --git a/src/parser.js b/src/parser.js index 2895ce0..eea506e 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1123,7 +1123,7 @@ PEG.parser = (function(){ var pos0; - if (input.substr(pos).match(/^[^{}]/) !== null) { + if (/^[^{}]/.test(input.substr(pos))) { result0 = input.charAt(pos); pos++; } else { @@ -3023,7 +3023,7 @@ PEG.parser = (function(){ var pos0; - if (input.substr(pos).match(/^[0-9]/) !== null) { + if (/^[0-9]/.test(input.substr(pos))) { result0 = input.charAt(pos); pos++; } else { @@ -3054,7 +3054,7 @@ PEG.parser = (function(){ var pos0; - if (input.substr(pos).match(/^[0-9a-fA-F]/) !== null) { + if (/^[0-9a-fA-F]/.test(input.substr(pos))) { result0 = input.charAt(pos); pos++; } else { @@ -3112,7 +3112,7 @@ PEG.parser = (function(){ var pos0; - if (input.substr(pos).match(/^[a-z]/) !== null) { + if (/^[a-z]/.test(input.substr(pos))) { result0 = input.charAt(pos); pos++; } else { @@ -3143,7 +3143,7 @@ PEG.parser = (function(){ var pos0; - if (input.substr(pos).match(/^[A-Z]/) !== null) { + if (/^[A-Z]/.test(input.substr(pos))) { result0 = input.charAt(pos); pos++; } else { @@ -3571,7 +3571,7 @@ PEG.parser = (function(){ var pos0; - if (input.substr(pos).match(/^[\n\r\u2028\u2029]/) !== null) { + if (/^[\n\r\u2028\u2029]/.test(input.substr(pos))) { result0 = input.charAt(pos); pos++; } else { @@ -3602,7 +3602,7 @@ PEG.parser = (function(){ var pos0; reportFailures++; - if (input.substr(pos).match(/^[ \xA0\uFEFF\u1680\u180E\u2000-\u200A\u202F\u205F\u3000]/) !== null) { + if (/^[ \xA0\uFEFF\u1680\u180E\u2000-\u200A\u202F\u205F\u3000]/.test(input.substr(pos))) { result0 = input.charAt(pos); pos++; } else {