From c6673887e7b16c01402be9effcf61df7d225d41c Mon Sep 17 00:00:00 2001 From: Nathaniel van Diepen Date: Thu, 31 Mar 2016 16:47:57 -0600 Subject: [PATCH] Make tokenizer faster. --- super-tiny-compiler-unannotated.js | 6 +++--- super-tiny-compiler.js | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/super-tiny-compiler-unannotated.js b/super-tiny-compiler-unannotated.js index 6fb0a9c..dc2ad74 100644 --- a/super-tiny-compiler-unannotated.js +++ b/super-tiny-compiler-unannotated.js @@ -1,6 +1,9 @@ function tokenizer(input) { var current = 0; var tokens = []; + var WHITESPACE = /\s/; + var NUMBERS = /[0-9]/; + var LETTERS = /[a-zA-Z]/; while (current < input.length) { var char = input[current]; @@ -23,13 +26,11 @@ function tokenizer(input) { continue; } - var WHITESPACE = /\s/; if (WHITESPACE.test(char)) { current++; continue; } - var NUMBERS = /[0-9]/; if (NUMBERS.test(char)) { var value = ''; @@ -46,7 +47,6 @@ function tokenizer(input) { continue; } - var LETTERS = /[a-zA-Z]/; if (LETTERS.test(char)) { var value = ''; diff --git a/super-tiny-compiler.js b/super-tiny-compiler.js index e37f1a4..fc18c65 100644 --- a/super-tiny-compiler.js +++ b/super-tiny-compiler.js @@ -348,6 +348,11 @@ function tokenizer(input) { // And a `tokens` array for pushing our tokens to. var tokens = []; + // Regex tests for later + var WHITESPACE = /\s/; + var NUMBERS = /[0-9]/; + var LETTERS = /[a-zA-Z]/; + // We start by creating a `while` loop where we are setting up our `current` // variable to be incremented as much as we want `inside` the loop. // @@ -398,7 +403,6 @@ function tokenizer(input) { // // So here we're just going to test for existence and if it does exist we're // going to just `continue` on. - var WHITESPACE = /\s/; if (WHITESPACE.test(char)) { current++; continue; @@ -413,7 +417,6 @@ function tokenizer(input) { // Only two separate tokens // // So we start this off when we encounter the first number in a sequence. - var NUMBERS = /[0-9]/; if (NUMBERS.test(char)) { // We're going to create a `value` string that we are going to push @@ -446,7 +449,6 @@ function tokenizer(input) { // ^^^ // Name token // - var LETTERS = /[a-zA-Z]/; if (LETTERS.test(char)) { var value = '';