Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Commit

Permalink
Improve performance, ref: ae9bc86
Browse files Browse the repository at this point in the history
Treat 1 char queries same as '', and dont search, just put all emojis
  • Loading branch information
muan committed Jan 30, 2016
1 parent d0dd4e4 commit 6c45a0a
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions app/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ var directions = {

searchInput.focus()
search('')
searchInput.addEventListener('input', function (evt) {
if (this.value.length > 1 || this.value.charCodeAt() > 255) search(this.value)
searchInput.addEventListener('input', function () {
search(this.value)
})

document.addEventListener('mousewheel', function (e) {
Expand Down Expand Up @@ -85,25 +85,34 @@ function search (query) {
clearTimeout(searching)
}
searching = setTimeout(function () {
var results = (Object.keys(index).filter(function matchQuery (keyword) {
return keyword.match(query)
})).map(function (keyword) {
return index[keyword]
}).join().split(',').filter(function filterUniqueResults (emoji, pos, arr) {
return emoji && arr.indexOf(emoji) === pos
}).sort(function sortResults (a, b) {
return emojikeys.indexOf(a) - emojikeys.indexOf(b)
}).map(function generateMarkup (name) {
var unicode = (emojilib[name]['char'] || '--')
var result = '<button type="button" class="emoji" aria-label="' + name + '">' + unicode + '</button>'
return result
}).join('')

document.querySelector('.js-results').innerHTML = results
var results
if (query.length === 0 || (query.length === 1 && query.charCodeAt() <= 255)) {
results = emojikeys
} else {
results = (Object.keys(index).filter(function matchQuery (keyword) {
return keyword.match(query)
})).map(function (keyword) {
return index[keyword]
}).join().split(',').filter(function filterUniqueResults (emoji, pos, arr) {
return emoji && arr.indexOf(emoji) === pos
}).sort(function sortResults (a, b) {
return emojikeys.indexOf(a) - emojikeys.indexOf(b)
})
}

document.querySelector('.js-results').innerHTML = generateMarkup(results)
if (document.querySelector('.emoji')) document.querySelector('.emoji').scrollIntoViewIfNeeded()
}, 100)
}

function generateMarkup (emojiNameArray) {
return emojiNameArray.map(function (name) {
var unicode = (emojilib[name]['char'] || '--')
var result = '<button type="button" class="emoji" aria-label="' + name + '">' + unicode + '</button>'
return result
}).join('')
}

function buildIndex () {
var keywords = {}
emojikeys.forEach(function (name) {
Expand Down

0 comments on commit 6c45a0a

Please sign in to comment.