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

View file

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