Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve search performance #1385

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions frontend/components/badge-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ const Category = ({ category, examples, baseUri, longCache, onClick }) => (
<table className="badge">
<tbody>
{
examples.map((badgeData, i) => (
examples.map(badgeData => (
<Badge
key={i}
key={badgeData.key}
{...badgeData}
baseUri={baseUri}
longCache={longCache}
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/suggestion-and-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class SuggestionAndSearch extends React.Component {

constructor(props) {
super(props);
this.queryChangedDebounced = debounce(props.queryChanged, 500, { leading: true });
this.queryChangedDebounced = debounce(props.queryChanged, 50, { leading: true });
}

state = {
Expand Down
32 changes: 24 additions & 8 deletions frontend/lib/filter-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@ function exampleMatchesRegex(example, regex) {
return regex.test(haystack);
}

export default function filterExamples(examples, query) {
if (! query) {
return examples;
function predicateFromQuery(query) {
if (query) {
const regex = new RegExp(query, 'i'); // Case-insensitive.
return example => exampleMatchesRegex(example, regex);
} else {
return () => true;
}
}

const regex = new RegExp(query, 'i'); // Case-insensitive.

return examples
export function mapExamples(categories, iteratee) {
return categories
.map(({ category, examples }) => ({
category,
examples: examples.filter(ex => exampleMatchesRegex(ex, regex)),
examples: iteratee(examples),
}))
// Remove empty sections.
// Remove empty categories.
.filter(({ category, examples }) => examples.length > 0);
}

// Assign each example a unique ID.
export function prepareExamples(categories) {
let nextKey = 0;
return mapExamples(categories, examples => examples.map(example => Object.assign(example, {
key: nextKey++,
})));
}

export function filterExamples(categories, query) {
const predicate = predicateFromQuery(query);
return mapExamples(categories, examples => examples.filter(predicate));
}
5 changes: 3 additions & 2 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import MarkupModal from '../frontend/components/markup-modal';
import Usage from '../frontend/components/usage';
import Footer from '../frontend/components/footer';
import badgeExampleData from '../lib/all-badge-examples';
import filterExamples from '../frontend/lib/filter-examples';
import { prepareExamples, filterExamples } from '../frontend/lib/filter-examples';

const baseUri = process.env.BASE_URL;
const longCache = envFlag(process.env.LONG_CACHE, false);
const preparedExamples = prepareExamples(badgeExampleData);

export default class IndexPage extends React.Component {
state = { query: null, example: null };
Expand All @@ -22,7 +23,7 @@ export default class IndexPage extends React.Component {
// adjusting visibility of the elements rather than removing them from the
// DOM and recreating them, as this does now. That's what the original
// code did.
const filteredExamples = filterExamples(badgeExampleData, this.state.query);
const filteredExamples = filterExamples(preparedExamples, this.state.query);

return (
<div>
Expand Down