From 73923cbb7f92e13f6eee6cadb42dc88825b86463 Mon Sep 17 00:00:00 2001 From: Nathaniel Hunter <42shadow42@gmail.com> Date: Fri, 24 Jun 2022 21:34:52 -0500 Subject: [PATCH 1/5] Optimize whitespace trimming --- lib/handlers/text.js | 7 ++++++- test/text.js | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/handlers/text.js b/lib/handlers/text.js index cb62d3d..0faa7d7 100644 --- a/lib/handlers/text.js +++ b/lib/handlers/text.js @@ -12,6 +12,11 @@ import {u} from 'unist-builder' export function text(h, node) { return h.augment( node, - u('text', String(node.value).replace(/[ \t]*(\r?\n|\r)[ \t]*/g, '$1')) + u( + 'text', + String(node.value) + .replace(/[ \t]*$/gm, '') + .replace(/^[ \t]*/gm, '') + ) ) } diff --git a/test/text.js b/test/text.js index 995f273..ad483ca 100644 --- a/test/text.js +++ b/test/text.js @@ -63,3 +63,13 @@ test('Nodes', (t) => { t.end() }) + +test('efficiency', {timeout: 1000}, (t) => { + t.deepEqual( + toHast(u('text', `1${' '.repeat(70_000)}2`)), + u('text', `1${' '.repeat(70_000)}2`), + 'should be efficient on excessive whitespace' + ) + + t.end() +}) From af68202536257072a1c45fd933220608f5a427dd Mon Sep 17 00:00:00 2001 From: Nathaniel Hunter <42shadow42@gmail.com> Date: Fri, 24 Jun 2022 22:37:40 -0500 Subject: [PATCH 2/5] Restructured test so it failed correctly, fixed code to pass test --- lib/handlers/text.js | 8 +++++++- test/text.js | 11 +++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/handlers/text.js b/lib/handlers/text.js index 0faa7d7..cf18a99 100644 --- a/lib/handlers/text.js +++ b/lib/handlers/text.js @@ -15,8 +15,14 @@ export function text(h, node) { u( 'text', String(node.value) - .replace(/[ \t]*$/gm, '') .replace(/^[ \t]*/gm, '') + .split('') + .reverse() + .join('') + .replace(/^[ \t]*/gm, '') + .split('') + .reverse() + .join('') ) ) } diff --git a/test/text.js b/test/text.js index ad483ca..581c3b8 100644 --- a/test/text.js +++ b/test/text.js @@ -64,12 +64,19 @@ test('Nodes', (t) => { t.end() }) -test('efficiency', {timeout: 1000}, (t) => { +test('Nodes Efficiency', (t) => { + const timeout = setTimeout(() => { + t.fail('should process lots of whitespace efficiently') + }, 10) + t.deepEqual( toHast(u('text', `1${' '.repeat(70_000)}2`)), u('text', `1${' '.repeat(70_000)}2`), 'should be efficient on excessive whitespace' ) - t.end() + setTimeout(() => { + clearTimeout(timeout) + t.end() + }, 0) }) From e54369f30d16526e7e027f7b4066093b6ef5070a Mon Sep 17 00:00:00 2001 From: Nathaniel Hunter <42shadow42@gmail.com> Date: Sat, 25 Jun 2022 09:51:20 -0500 Subject: [PATCH 3/5] Optimized replacement further by only considering places where replacements would actually occur --- lib/handlers/text.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/handlers/text.js b/lib/handlers/text.js index cf18a99..1f7d15c 100644 --- a/lib/handlers/text.js +++ b/lib/handlers/text.js @@ -15,11 +15,11 @@ export function text(h, node) { u( 'text', String(node.value) - .replace(/^[ \t]*/gm, '') + .replace(/^[ \t]+/gm, '') .split('') .reverse() .join('') - .replace(/^[ \t]*/gm, '') + .replace(/^[ \t]+/gm, '') .split('') .reverse() .join('') From 266578e9013f0c2d68e481ff8ac66890f27b09e2 Mon Sep 17 00:00:00 2001 From: Nathaniel Hunter <42shadow42@gmail.com> Date: Sun, 3 Jul 2022 19:25:07 -0500 Subject: [PATCH 4/5] Updated to use trim-lines dependency for trimming --- lib/handlers/text.js | 17 ++--------------- package.json | 1 + test/text.js | 17 ----------------- 3 files changed, 3 insertions(+), 32 deletions(-) diff --git a/lib/handlers/text.js b/lib/handlers/text.js index 1f7d15c..923fed1 100644 --- a/lib/handlers/text.js +++ b/lib/handlers/text.js @@ -3,6 +3,7 @@ * @typedef {import('../index.js').Handler} Handler */ +import {trimLines} from 'trim-lines' import {u} from 'unist-builder' /** @@ -10,19 +11,5 @@ import {u} from 'unist-builder' * @param {Text} node */ export function text(h, node) { - return h.augment( - node, - u( - 'text', - String(node.value) - .replace(/^[ \t]+/gm, '') - .split('') - .reverse() - .join('') - .replace(/^[ \t]+/gm, '') - .split('') - .reverse() - .join('') - ) - ) + return h.augment(node, u('text', trimLines(String(node.value)))) } diff --git a/package.json b/package.json index e57d400..0b0a199 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "mdast-util-definitions": "^5.0.0", "mdurl": "^1.0.0", "micromark-util-sanitize-uri": "^1.0.0", + "trim-lines": "^3.0.1", "unist-builder": "^3.0.0", "unist-util-generated": "^2.0.0", "unist-util-position": "^4.0.0", diff --git a/test/text.js b/test/text.js index 581c3b8..995f273 100644 --- a/test/text.js +++ b/test/text.js @@ -63,20 +63,3 @@ test('Nodes', (t) => { t.end() }) - -test('Nodes Efficiency', (t) => { - const timeout = setTimeout(() => { - t.fail('should process lots of whitespace efficiently') - }, 10) - - t.deepEqual( - toHast(u('text', `1${' '.repeat(70_000)}2`)), - u('text', `1${' '.repeat(70_000)}2`), - 'should be efficient on excessive whitespace' - ) - - setTimeout(() => { - clearTimeout(timeout) - t.end() - }, 0) -}) From fcdd6a741bb1496679d96aed6f3b9c36b0a1c97a Mon Sep 17 00:00:00 2001 From: Titus Date: Mon, 4 Jul 2022 21:00:04 +0200 Subject: [PATCH 5/5] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0b0a199..c3fb309 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "mdast-util-definitions": "^5.0.0", "mdurl": "^1.0.0", "micromark-util-sanitize-uri": "^1.0.0", - "trim-lines": "^3.0.1", + "trim-lines": "^3.0.0", "unist-builder": "^3.0.0", "unist-util-generated": "^2.0.0", "unist-util-position": "^4.0.0",