From 4b75275680b5272774fc639016536a3999ac17fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ketil=20=C3=98vreb=C3=B8?= Date: Fri, 15 Mar 2024 08:12:09 +0100 Subject: [PATCH 1/3] Add benchmark for createTrie --- test/benchmark.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/benchmark.js b/test/benchmark.js index 6c4e788..bbbc7de 100644 --- a/test/benchmark.js +++ b/test/benchmark.js @@ -55,6 +55,8 @@ suite.add('Hypher', hypherDictionary, { setup: hypherSetup }); +suite.add('Hypher (createTrie)', hypherSetup); + suite.add('Hyphenator', hyphenatorDictionary, { setup: hyphenatorSetup }); From 84ea86ee7ffb53f90e9b18de25e2cef6740d5292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ketil=20=C3=98vreb=C3=B8?= Date: Fri, 15 Mar 2024 10:41:12 +0100 Subject: [PATCH 2/3] Optimize Hypher --- lib/hypher.js | 51 +++++++++++++++++++++++---------------------- test/hypher-test.js | 4 ++-- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/lib/hypher.js b/lib/hypher.js index 2278d7b..72c12a3 100644 --- a/lib/hypher.js +++ b/lib/hypher.js @@ -50,11 +50,6 @@ Hypher.TrieNode; Hypher.prototype.createTrie = function (patternObject) { var size = 0, i = 0, - c = 0, - p = 0, - chars = null, - points = null, - codePoint = null, t = null, tree = { _points: [] @@ -63,27 +58,35 @@ Hypher.prototype.createTrie = function (patternObject) { for (size in patternObject) { if (patternObject.hasOwnProperty(size)) { - patterns = patternObject[size].match(new RegExp('.{1,' + (+size) + '}', 'g')); - - for (i = 0; i < patterns.length; i += 1) { - chars = patterns[i].replace(/[0-9]/g, '').split(''); - points = patterns[i].split(/\D/); + size = +size; + for (i = 0; i < patternObject[size].length; i += size) { t = tree; - for (c = 0; c < chars.length; c += 1) { - codePoint = chars[c].charCodeAt(0); + const points = []; + let prev = 0; - if (!t[codePoint]) { - t[codePoint] = {}; + for (let j = 0; j < size; j++) { + const char = patternObject[size][i + j]; + if (!char) { + break; + } + const codePoint = char.charCodeAt(0); + if (codePoint >= 48 && codePoint <= 57) { + points.push(+char); + } else { + if (prev < 48 || prev > 57) { + points.push(0); + } + + if (!t[codePoint]) { + t[codePoint] = {}; + } + t = t[codePoint]; } - t = t[codePoint]; + prev = codePoint; } - t._points = []; - - for (p = 0; p < points.length; p += 1) { - t._points[p] = points[p] || 0; - } + t._points = points; } } } @@ -129,7 +132,6 @@ Hypher.prototype.hyphenateText = function (str, minLength) { Hypher.prototype.hyphenate = function (word) { var characters, characterPoints = [], - originalCharacters, i, j, k, @@ -153,8 +155,7 @@ Hypher.prototype.hyphenate = function (word) { word = '_' + word + '_'; - characters = word.toLowerCase().split(''); - originalCharacters = word.split(''); + characters = word.toLowerCase(); wordLength = characters.length; for (i = 0; i < wordLength; i += 1) { @@ -182,9 +183,9 @@ Hypher.prototype.hyphenate = function (word) { for (i = 1; i < wordLength - 1; i += 1) { if (i > this.leftMin && i < (wordLength - this.rightMin) && points[i] % 2) { - result.push(originalCharacters[i]); + result.push(word[i]); } else { - result[result.length - 1] += originalCharacters[i]; + result[result.length - 1] += word[i]; } } diff --git a/test/hypher-test.js b/test/hypher-test.js index 3235a08..1aea7b0 100644 --- a/test/hypher-test.js +++ b/test/hypher-test.js @@ -70,13 +70,13 @@ vows.describe('Hypher').addBatch({ 97: { _points: [0, 1], 98: { - _points: [0, 2, 0] + _points: [0, 2] } }, 98: { _points: [0, 2], 99: { - _points: [0, 3, 0] + _points: [0, 3] } }, _points: [] From 802b19956920f70a385c60224dbd305553dc0964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ketil=20=C3=98vreb=C3=B8?= Date: Fri, 15 Mar 2024 11:07:32 +0100 Subject: [PATCH 3/3] Remove unused dependency --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 561b594..cf4eb71 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,6 @@ "author": "Bram Stein (http://www.bramstein.com)", "devDependencies": { "benchmark": "=0.1.347", - "microtime": "^2.1.3", "vows": ">=0.5.6" }, "directories": {