Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #9582 from brave/9581
Browse files Browse the repository at this point in the history
Optimize top site order lookups
  • Loading branch information
bsclifton authored Jun 20, 2017
2 parents 22d2451 + 6e28a1c commit c43bfa4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
18 changes: 15 additions & 3 deletions js/data/top500.js → app/common/data/topSites.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
}
9 changes: 4 additions & 5 deletions app/common/lib/suggestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions test/unit/app/common/data/topSitesTest.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})

0 comments on commit c43bfa4

Please sign in to comment.