From 6e28a1c39dcaa221a324e5228b0f3ffe94fb4aff Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Mon, 19 Jun 2017 17:09:09 -0400 Subject: [PATCH] Optimize top site order lookups Fix #9581 --- .../top500.js => app/common/data/topSites.js | 18 +++++++++-- app/common/lib/suggestion.js | 9 +++--- test/unit/app/common/data/topSitesTest.js | 30 +++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) rename js/data/top500.js => app/common/data/topSites.js (96%) create mode 100644 test/unit/app/common/data/topSitesTest.js diff --git a/js/data/top500.js b/app/common/data/topSites.js similarity index 96% rename from js/data/top500.js rename to app/common/data/topSites.js index e04001d0a17..c05f8006dc5 100644 --- a/js/data/top500.js +++ b/app/common/data/topSites.js @@ -2,8 +2,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -// Top 500 alexa sites sorted by popularity -const top500 = [ +// Top sites +const topSites = [ 'google.com', 'gmail.com', 'mail.google.com', @@ -507,4 +507,16 @@ const top500 = [ 'brave.com' ] -module.exports = top500 +const siteOrderMap = topSites.reduce( + (result, site, i) => { + result[site] = i + 1 + return result + }, {} +) + +const getSiteOrder = (site) => (siteOrderMap[site] || Number.MAX_SAFE_INTEGER) + +module.exports = { + topSites, + getSiteOrder +} diff --git a/app/common/lib/suggestion.js b/app/common/lib/suggestion.js index 5ae461c0020..4af51274cef 100644 --- a/app/common/lib/suggestion.js +++ b/app/common/lib/suggestion.js @@ -13,7 +13,7 @@ const suggestionTypes = require('../../../js/constants/suggestionTypes') const getSetting = require('../../../js/settings').getSetting const settings = require('../../../js/constants/settings') const config = require('../../../js/constants/config') -const top500 = require('../../../js/data/top500') +const {topSites, getSiteOrder} = require('../data/topSites') const fetchSearchSuggestions = require('./fetchSearchSuggestions') const {getFrameByTabId, getTabsByWindowId} = require('../../common/state/tabState') const {query} = require('./siteSuggestions') @@ -299,9 +299,8 @@ const getSortByDomainForHosts = (userInputHost) => { } } - // The list here is sufficiently small to not be a perf concern - const topPos1 = top500.indexOf(host1) - const topPos2 = top500.indexOf(host2) + const topPos1 = getSiteOrder(host1) + const topPos2 = getSiteOrder(host2) if (topPos1 !== -1 && topPos2 === -1) { return -1 @@ -582,7 +581,7 @@ const getAlexaSuggestions = (state, urlLocationLower) => { let suggestionsList = Immutable.List() if (getSetting(settings.TOPSITE_SUGGESTIONS)) { suggestionsList = mapListToElements({ - data: top500, + data: topSites, maxResults: config.urlBarSuggestions.maxTopSites, type: suggestionTypes.TOP_SITE, sortHandler diff --git a/test/unit/app/common/data/topSitesTest.js b/test/unit/app/common/data/topSitesTest.js new file mode 100644 index 00000000000..55e0fd1d550 --- /dev/null +++ b/test/unit/app/common/data/topSitesTest.js @@ -0,0 +1,30 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ +/* global describe, it */ + +const assert = require('assert') + +require('../../../braveUnit') +const {topSites, getSiteOrder} = require('../../../../../app/common/data/topSites') + +describe('topSites', function () { + describe('topSites', function () { + it('exports an array with top sites', function () { + assert.equal(topSites.constructor, Array) + assert.ok(topSites.length > 0) + assert.equal(topSites[0], 'google.com') + }) + }) + describe('getSiteOrder', function () { + it('obtains the first site\'s order', function () { + assert.equal(getSiteOrder('google.com'), 1) + }) + it('obtains an arbigrary site\'s order', function () { + assert.equal(getSiteOrder('calendar.google.com'), 4) + }) + it('orders unknown sites as max int', function () { + assert.equal(getSiteOrder('bradhatesprimes.com'), 9007199254740991) + }) + }) +})