This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/unit/lib/suffix-trie-test.js
David Taylor 135fdd59ed
PERF: Improve JS app boot speed by optimizing customResolve() (#14990)
Time spent in the 'find module with suffix' portion of our `customResolve` function were adding up to around 100ms-150ms when booting the app. This time is spread over 150+ calls, so it's not immediately obvious in flamegraphs.

This commit implements a (reversed) [Trie](https://en.wikipedia.org/wiki/Trie) which enables fast suffix-based lookups on a list of strings.

In my tests, this requires < 5ms to initialize, and brings the cumulative 'find module with suffix' time down to `< 5ms`. This corresponds to a ~100ms improvement in LCP metrics in my browser.

The only behavior change is to remove support for module filenames which are **not** dasherized. I haven't found any core/theme/plugin modules which are not dasherized in their filenames.
2021-11-18 16:38:00 +00:00

31 lines
1.0 KiB
JavaScript

import { module, test } from "qunit";
import SuffixTrie from "discourse-common/lib/suffix-trie";
module("Unit | SuffixTrie", function () {
test("SuffixTrie", function (assert) {
const t = new SuffixTrie("/");
t.add("a/b/c/d");
t.add("b/a/c/d");
t.add("c/b/a/d");
t.add("d/c/b/a");
t.add("a/b/c/d/");
t.add("/a/b/c/d/");
// Simple lookups
assert.deepEqual(t.withSuffix("d"), ["a/b/c/d", "b/a/c/d", "c/b/a/d"]);
assert.deepEqual(t.withSuffix("c/d"), ["a/b/c/d", "b/a/c/d"]);
assert.deepEqual(t.withSuffix("b/c/d"), ["a/b/c/d"]);
assert.deepEqual(t.withSuffix("a/b/c/d"), ["a/b/c/d"]);
assert.deepEqual(t.withSuffix("b/a"), ["d/c/b/a"]);
// With leading/trailing delimiters
assert.deepEqual(t.withSuffix("c/d/"), ["a/b/c/d/", "/a/b/c/d/"]);
assert.deepEqual(t.withSuffix("/a/b/c/d/"), ["/a/b/c/d/"]);
// Limited lookups
assert.deepEqual(t.withSuffix("d", 1), ["a/b/c/d"]);
assert.deepEqual(t.withSuffix("d", 2), ["a/b/c/d", "b/a/c/d"]);
});
});