huang47 / nodejs-html-truncate

truncate html text and keep tag safe
77 stars 30 forks source link

Long attribute keys take a very long time to process #26

Open brian-c opened 7 years ago

brian-c commented 7 years ago

This code times a bunch of attribute keys:

const truncate = require('html-truncate');

function timeTruncate(html) {
    const start = Date.now();
    truncate(html, 0);
    const end = Date.now();
    console.log(`${end - start}ms for ${html}`)
}

timeTruncate('<div a="x"></div>');
timeTruncate('<div a00000009="x"></div>');
timeTruncate('<div a000000010="x"></div>');
timeTruncate('<div a0000000011="x"></div>');
timeTruncate('<div a00000000012="x"></div>');
timeTruncate('<div a000000000013="x"></div>');
timeTruncate('<div a0000000000014="x"></div>');
timeTruncate('<div a00000000000015="x"></div>');
timeTruncate('<div a000000000000016="x"></div>');
timeTruncate('<div a0000000000000017="x"></div>');
timeTruncate('<div a00000000000000018="x"></div>');
timeTruncate('<div a000000000000000019="x"></div>');
timeTruncate('<div a0000000000000000020="x"></div>');
timeTruncate('<div a00000000000000000021="x"></div>');
timeTruncate('<div a000000000000000000022="x"></div>');
timeTruncate('<div a0000000000000000000023="x"></div>');
timeTruncate('<div a00000000000000000000024="x"></div>');
timeTruncate('<div a000000000000000000000025="x"></div>');

The results, with html-truncate 1.2.2 and Node v8.9.0:

1ms for <div a="x"></div>
1ms for <div a00000009="x"></div>
0ms for <div a000000010="x"></div>
1ms for <div a0000000011="x"></div>
2ms for <div a00000000012="x"></div>
3ms for <div a000000000013="x"></div>
5ms for <div a0000000000014="x"></div>
12ms for <div a00000000000015="x"></div>
22ms for <div a000000000000016="x"></div>
34ms for <div a0000000000000017="x"></div>
67ms for <div a00000000000000018="x"></div>
122ms for <div a000000000000000019="x"></div>
230ms for <div a0000000000000000020="x"></div>
450ms for <div a00000000000000000021="x"></div>
880ms for <div a000000000000000000022="x"></div>
1828ms for <div a0000000000000000000023="x"></div>
4199ms for <div a00000000000000000000024="x"></div>
7585ms for <div a000000000000000000000025="x"></div>

Things start getting a little out of hand beyond ten characters.

Possibly related to #24?