search.js: slice earlier when building typeahead objects

Slicing earlier means less work to do on items that were gonna
be sliced anyhow.

(imported from commit 743c51c478679a14f101e6cf7c14eafb9d719a58)
This commit is contained in:
Steve Howell 2013-07-16 16:53:55 -04:00
parent e6e56ef97e
commit c201354e6b
1 changed files with 9 additions and 5 deletions

View File

@ -135,7 +135,7 @@ function highlight_person(query, person) {
return hilite(query, person.full_name) + " <" + hilite(query, person.email) + ">"; return hilite(query, person.full_name) + " <" + hilite(query, person.email) + ">";
} }
function get_stream_suggestions(query) { function get_stream_suggestions(query, max_num) {
var streams = subs.subscribed_streams(); var streams = subs.subscribed_streams();
streams = $.grep(streams, function (stream) { streams = $.grep(streams, function (stream) {
@ -148,6 +148,8 @@ function get_stream_suggestions(query) {
objs = typeahead_helper.sorter(query, objs, get_query); objs = typeahead_helper.sorter(query, objs, get_query);
objs = objs.slice(0, max_num);
$.each(objs, function (idx, obj) { $.each(objs, function (idx, obj) {
var prefix = 'Narrow to stream'; var prefix = 'Narrow to stream';
var stream = obj.query; var stream = obj.query;
@ -159,7 +161,7 @@ function get_stream_suggestions(query) {
return objs; return objs;
} }
function get_person_suggestions(all_people, query, action) { function get_person_suggestions(all_people, query, action, max_num) {
var people = $.grep(all_people, function (person) { var people = $.grep(all_people, function (person) {
return person_matches_query(person, query); return person_matches_query(person, query);
}); });
@ -173,6 +175,8 @@ function get_person_suggestions(all_people, query, action) {
return typeahead_helper.compare_by_pms(get_query(x), get_query(y)); return typeahead_helper.compare_by_pms(get_query(x), get_query(y));
}); });
objs = objs.slice(0, max_num);
$.each(objs, function (idx, obj) { $.each(objs, function (idx, obj) {
var prefix; var prefix;
var person; var person;
@ -212,16 +216,16 @@ exports.initialize = function () {
return []; return [];
} }
var stream_suggestions = get_stream_suggestions(query).slice(0,4); var stream_suggestions = get_stream_suggestions(query, 4);
result = result.concat(stream_suggestions); result = result.concat(stream_suggestions);
var people = page_params.people_list; var people = page_params.people_list;
var person_suggestions; var person_suggestions;
person_suggestions = get_person_suggestions(people, query, 'private_message').slice(0, 4); person_suggestions = get_person_suggestions(people, query, 'private_message', 4);
result = result.concat(person_suggestions); result = result.concat(person_suggestions);
person_suggestions = get_person_suggestions(people, query, 'sender').slice(0, 4); person_suggestions = get_person_suggestions(people, query, 'sender', 4);
result = result.concat(person_suggestions); result = result.concat(person_suggestions);
// We can't send typeahead objects, only strings. // We can't send typeahead objects, only strings.