perf(accounts): Use cached contact objects in accountForEmail

This method is called for every contact on a thread in the thread list, so I figured we should see if one version was faster. I ran this test code:

```
d = Date.now(); for(var ii = 0; ii < 10000; ii ++){ $n.AccountStore.accountForEmail("ben.gotow@gmail.com") }; console.log(Date.now() - d);
```

The other approach which calls meUsingAlias takes `3784ms`, and this version which uses the alias contacts cached in aliases() only takes `264ms`. Confirmed that the tests still pass.
This commit is contained in:
Ben Gotow 2016-02-07 11:23:19 -08:00
parent dce7573218
commit 53266ea408
2 changed files with 12 additions and 19 deletions

View file

@ -132,16 +132,9 @@ describe "Contact", ->
c1 = new Contact {email: @account.emailAddress.toUpperCase()}
expect(c1.isMe()).toBe(true)
it "also matches any aliases you've created", ->
spyOn(AccountStore, 'accounts').andReturn [
new Account
provider: "gmail"
accountId: TEST_ACCOUNT_ID
aliases: ["Ben Other <ben22@nylas.com>"]
emailAddress: 'ben@nylas.com'
]
c1 = new Contact {email: 'ben22@nylas.com'}
it "it calls through to accountForEmail", ->
c1 = new Contact {email: @account.emailAddress}
acct = new Account()
spyOn(AccountStore, 'accountForEmail').andReturn(acct)
expect(c1.isMe()).toBe(true)
c1 = new Contact {email: 'ben23@nylas.com'}
expect(c1.isMe()).toBe(false)
expect(AccountStore.accountForEmail).toHaveBeenCalled()

View file

@ -131,13 +131,13 @@ class AccountStore extends NylasStore
# Public: Returns the {Account} for the given email address, or null.
accountForEmail: (email) =>
@_cachedGetter "accountForEmail:#{email}", =>
_.find @accounts(), (account) ->
return true if Utils.emailIsEquivalent(email, account.emailAddress)
for alias in account.aliases
aliasContact = account.meUsingAlias(alias)
return true if Utils.emailIsEquivalent(email, aliasContact.email)
return false
for account in @accounts()
if Utils.emailIsEquivalent(email, account.emailAddress)
return account
for alias in @aliases()
if Utils.emailIsEquivalent(email, alias.email)
return @accountForId(alias.accountId)
return null
# Public: Returns the {Account} for the given account id, or null.
accountForId: (id) =>