From 0711c7ea49ba80676a64efefe414a26c16c725aa Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Fri, 27 Dec 2019 20:45:28 +0000 Subject: [PATCH] performance: Avoid dup calls to subscribed_streams(). In stream_sort.sort_groups, we now have the caller pass us in the list of streams, since they are getting them anyway. --- frontend_tests/node_tests/stream_sort.js | 15 ++++++++++----- static/js/stream_list.js | 2 +- static/js/stream_sort.js | 3 +-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/frontend_tests/node_tests/stream_sort.js b/frontend_tests/node_tests/stream_sort.js index 3dee2fc2d2..7e49874805 100644 --- a/frontend_tests/node_tests/stream_sort.js +++ b/frontend_tests/node_tests/stream_sort.js @@ -45,13 +45,18 @@ stream_data.add_sub(pneumonia.name, pneumonia); stream_data.add_sub(clarinet.name, clarinet); stream_data.add_sub(weaving.name, weaving); +function sort_groups(query) { + const streams = stream_data.subscribed_streams(); + return stream_sort.sort_groups(streams, query); +} + with_overrides(function (override) { override('stream_data.is_active', function (sub) { return sub.name !== "pneumonia"; }); // Test sorting into categories/alphabetized - let sorted = stream_sort.sort_groups(""); + let sorted = sort_groups(""); assert.deepEqual(sorted.pinned_streams, ['scalene']); assert.deepEqual(sorted.normal_streams, ['clarinet', 'fast tortoise']); assert.deepEqual(sorted.dormant_streams, ['pneumonia']); @@ -66,7 +71,7 @@ with_overrides(function (override) { assert.equal(stream_sort.next_stream_id(pneumonia.stream_id), undefined); // Test filtering - sorted = stream_sort.sort_groups("s"); + sorted = sort_groups("s"); assert.deepEqual(sorted.pinned_streams, ['scalene']); assert.deepEqual(sorted.normal_streams, []); assert.deepEqual(sorted.dormant_streams, []); @@ -76,19 +81,19 @@ with_overrides(function (override) { assert.equal(stream_sort.next_stream_id(clarinet.stream_id), undefined); // Test searching entire word, case-insensitive - sorted = stream_sort.sort_groups("PnEuMoNiA"); + sorted = sort_groups("PnEuMoNiA"); assert.deepEqual(sorted.pinned_streams, []); assert.deepEqual(sorted.normal_streams, []); assert.deepEqual(sorted.dormant_streams, ['pneumonia']); // Test searching part of word - sorted = stream_sort.sort_groups("tortoise"); + sorted = sort_groups("tortoise"); assert.deepEqual(sorted.pinned_streams, []); assert.deepEqual(sorted.normal_streams, ['fast tortoise']); assert.deepEqual(sorted.dormant_streams, []); // Test searching stream with spaces - sorted = stream_sort.sort_groups("fast t"); + sorted = sort_groups("fast t"); assert.deepEqual(sorted.pinned_streams, []); assert.deepEqual(sorted.normal_streams, ['fast tortoise']); assert.deepEqual(sorted.dormant_streams, []); diff --git a/static/js/stream_list.js b/static/js/stream_list.js index 361a13259e..26089e1a66 100644 --- a/static/js/stream_list.js +++ b/static/js/stream_list.js @@ -91,7 +91,7 @@ exports.build_stream_list = function () { // The main logic to build the list is in stream_sort.js, and // we get three lists of streams (pinned/normal/dormant). - const stream_groups = stream_sort.sort_groups(get_search_term()); + const stream_groups = stream_sort.sort_groups(streams, get_search_term()); if (stream_groups.same_as_before) { return; diff --git a/static/js/stream_sort.js b/static/js/stream_sort.js index c97b39d708..d6082bff4a 100644 --- a/static/js/stream_sort.js +++ b/static/js/stream_sort.js @@ -33,8 +33,7 @@ function filter_streams_by_search(streams, search_term) { return filtered_streams; } -exports.sort_groups = function (search_term) { - let streams = stream_data.subscribed_streams(); +exports.sort_groups = function (streams, search_term) { if (streams.length === 0) { return; }